{
Type itemType = collectionContract.ItemType;
int i;
+
+ // This check does not exist in the original dynamic code,
+ // but there is no other way to check type mismatch.
+ // CollectionSerialization.ArrayContract() shows that it is required.
+ if (objLocal.GetType ().GetElementType () != itemType)
+ throw new InvalidCastException (string.Format ("Cannot cast array of {0} to array of {1}", objLocal.GetType ().GetElementType (), itemType));
+
ctx.IncrementArrayCount (writer, (Array) objLocal);
if (!TryWritePrimitiveArray(collectionContract.UnderlyingType, itemType, () => objLocal, itemName, itemNamespace))
}
else
{
+ // This check does not exist in the original dynamic code,
+ // but there is no other way to check type mismatch.
+ // CollectionSerialization.ArrayContract() shows that it is required.
+ if (!collectionContract.UnderlyingType.IsAssignableFrom (objLocal.GetType ()))
+ throw new InvalidCastException (string.Format ("Cannot cast {0} to {1}", objLocal.GetType (), collectionContract.UnderlyingType));
+
MethodInfo incrementCollectionCountMethod = null;
switch (collectionContract.Kind)
{