Browse Source

Fix bug 704813: just ignore members of type IEnumerable`1 without Add().

Atsushi Eno 14 years ago
parent
commit
2cc686a3af

+ 6 - 3
mcs/class/System.XML/System.Xml.Serialization/TypeData.cs

@@ -448,10 +448,13 @@ namespace System.Xml.Serialization
 		};
 
 #if NET_2_0
-		private Type GetGenericListItemType (Type type)
+		internal static Type GetGenericListItemType (Type type)
 		{
-			if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (ICollection<>))
-				return type.GetGenericArguments () [0];
+			if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (IEnumerable<>)) {
+				Type [] gatypes = type.GetGenericArguments ();
+				if (type.GetMethod ("Add", gatypes) != null)
+					return gatypes [0];
+			}
 			Type t = null;
 			foreach (Type i in type.GetInterfaces ())
 				if ((t = GetGenericListItemType (i)) != null)

+ 4 - 1
mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs

@@ -792,7 +792,10 @@ namespace System.Xml.Serialization {
 						XmlAttributes atts = attributeOverrides[type, prop.Name];
 						if (atts == null) atts = new XmlAttributes (prop);
 						if (atts.XmlIgnore) continue;
-						if (!prop.CanWrite && (TypeTranslator.GetTypeData (prop.PropertyType).SchemaType != SchemaTypes.Array || prop.PropertyType.IsArray)) continue;
+						if (!prop.CanWrite) {
+							if (prop.PropertyType.IsGenericType && TypeData.GetGenericListItemType (prop.PropertyType) == null) continue; // check this before calling GetTypeData() which raises error for missing Add(). See bug #704813.
+							if (TypeTranslator.GetTypeData (prop.PropertyType).SchemaType != SchemaTypes.Array || prop.PropertyType.IsArray) continue;
+						}
 						XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
 						member.DeclaringType = prop.DeclaringType;
 						members.Add(member);

+ 8 - 0
mcs/class/System.XML/Test/System.Xml.Serialization/XmlReflectionImporterTests.cs

@@ -12,6 +12,7 @@
 
 using System;
 using System.Collections;
+using System.IO;
 using System.Xml;
 using System.Xml.Schema;
 using System.Xml.Serialization;
@@ -1627,6 +1628,13 @@ namespace MonoTests.System.XmlSerialization
 		{
 			new XmlSerializer (typeof (MyCollection));
 		}
+
+		[Test]
+		public void Bug704813Type ()
+		{
+			var xs = new XmlSerializer (typeof (Bug704813Type));
+			xs.Serialize (TextWriter.Null, new Bug704813Type ());
+		}
 #endif
 
 		public class Employee : IXmlSerializable

+ 8 - 0
mcs/class/System.XML/Test/System.Xml.Serialization/XmlSerializerTestClasses.cs

@@ -959,5 +959,13 @@ namespace MonoTests.System.Xml.TestClasses
 		{
 		}
 	}
+
+	public class Bug704813Type
+	{
+		IEnumerable<string> foo = new List<string> ();
+		public IEnumerable<string> Foo {
+			get { return foo; }
+		}
+	}
 }