2 // System.Xml.Serialization.TypeData
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Lluis Sanchez Gual (lluis@ximian.com)
8 // (C) 2002 Ximian, Inc (http://www.ximian.com)
12 using System.Collections;
13 using System.Reflection;
15 namespace System.Xml.Serialization
17 internal class TypeData
25 TypeData listItemTypeData;
26 TypeData listTypeData;
28 public TypeData (Type type, string elementName, bool isPrimitive)
31 this.elementName = elementName;
32 this.typeName = type.Name;
33 this.fullTypeName = type.FullName;
36 sType = SchemaTypes.Primitive;
40 sType = SchemaTypes.Enum;
41 else if (typeof(IXmlSerializable).IsAssignableFrom (type))
42 sType = SchemaTypes.XmlSerializable;
43 else if (typeof (System.Xml.XmlNode).IsAssignableFrom (type))
44 sType = SchemaTypes.XmlNode;
45 else if (type.IsArray || typeof(IEnumerable).IsAssignableFrom (type))
46 sType = SchemaTypes.Array;
48 sType = SchemaTypes.Class;
52 internal TypeData (string typeName, string fullTypeName, string xmlType, SchemaTypes schemaType, TypeData listItemTypeData)
54 this.elementName = xmlType;
55 this.typeName = typeName;
56 this.fullTypeName = fullTypeName;
57 this.listItemTypeData = listItemTypeData;
58 this.sType = schemaType;
61 public string TypeName
82 public string FullTypeName
85 // return type.FullName.Replace ('+', '.');
90 public SchemaTypes SchemaType
97 public bool IsListType
99 get { return SchemaType == SchemaTypes.Array; }
102 public bool IsComplexType
106 return (SchemaType == SchemaTypes.Class ||
107 SchemaType == SchemaTypes.Array ||
108 SchemaType == SchemaTypes.Enum ||
109 SchemaType == SchemaTypes.XmlSerializable );
114 public TypeData ListItemTypeData
118 if (listItemTypeData == null && type != null)
119 listItemTypeData = TypeTranslator.GetTypeData (ListItemType);
120 return listItemTypeData;
124 public Type ListItemType
128 if (type == null) throw new InvalidOperationException ("Property ListItemType is not supported for custom types");
130 if (listItemType != null) return listItemType;
132 if (SchemaType != SchemaTypes.Array)
133 throw new InvalidOperationException (Type.FullName + " is not a collection");
134 else if (type.IsArray)
135 listItemType = type.GetElementType ();
136 else if (typeof(ICollection).IsAssignableFrom (type))
138 PropertyInfo prop = GetIndexerProperty (type);
139 if (prop == null) throw new InvalidOperationException ("You must implement a default accessor on " + type.FullName + " because it inherits from ICollection");
140 return prop.PropertyType;
143 return type.GetMethod ("Add").GetParameters()[0].ParameterType;
149 public TypeData ListTypeData
153 if (listTypeData != null) return listTypeData;
155 listTypeData = new TypeData (TypeName + "[]",
157 TypeTranslator.GetArrayName(XmlType),
158 SchemaTypes.Array, this);
165 public static PropertyInfo GetIndexerProperty (Type collectionType)
167 PropertyInfo[] props = collectionType.GetProperties (BindingFlags.Instance | BindingFlags.Public);
168 foreach (PropertyInfo prop in props)
170 ParameterInfo[] pi = prop.GetIndexParameters ();
171 if (pi != null && pi.Length == 1 && pi[0].ParameterType == typeof(int))