[S.R.Serialization] additional type check for collection serialization is also needed.
[mono.git] / mcs / class / System.Runtime.Serialization / ReferenceSources / XmlFormatWriterGenerator_static.cs
index ff9ff45f40f705064ab9bf210b56464063a0dc62..0ed9000d2cebbffe484ce0110a42a67d0c5d4f21 100644 (file)
@@ -195,6 +195,12 @@ namespace System.Runtime.Serialization
                        }
                        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)
                                {