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 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Reflection;
35 using System.Xml.Schema;
37 namespace System.Xml.Serialization
39 internal class TypeData
47 TypeData listItemTypeData;
48 TypeData listTypeData;
50 XmlSchemaPatternFacet facet;
51 bool hasPublicConstructor = true;
53 public TypeData (Type type, string elementName, bool isPrimitive) :
54 this(type, elementName, isPrimitive, null, null) {}
56 public TypeData (Type type, string elementName, bool isPrimitive, TypeData mappedType, XmlSchemaPatternFacet facet)
58 this.mappedType = mappedType;
61 this.typeName = type.Name;
62 this.fullTypeName = type.FullName.Replace ('+', '.');
65 sType = SchemaTypes.Primitive;
69 sType = SchemaTypes.Enum;
70 else if (typeof(IXmlSerializable).IsAssignableFrom (type))
71 sType = SchemaTypes.XmlSerializable;
72 else if (typeof (System.Xml.XmlNode).IsAssignableFrom (type))
73 sType = SchemaTypes.XmlNode;
74 else if (type.IsArray || typeof(IEnumerable).IsAssignableFrom (type))
75 sType = SchemaTypes.Array;
77 sType = SchemaTypes.Class;
81 this.elementName = TypeTranslator.GetArrayName (ListItemTypeData.XmlType);
83 this.elementName = elementName;
85 if (sType == SchemaTypes.Array || sType == SchemaTypes.Class) {
86 hasPublicConstructor = !type.IsInterface && (type.IsArray || type.GetConstructor (Type.EmptyTypes) != null || type.IsAbstract || type.IsValueType);
90 internal TypeData (string typeName, string fullTypeName, string xmlType, SchemaTypes schemaType, TypeData listItemTypeData)
92 this.elementName = xmlType;
93 this.typeName = typeName;
94 this.fullTypeName = fullTypeName.Replace ('+', '.');
95 this.listItemTypeData = listItemTypeData;
96 this.sType = schemaType;
97 this.hasPublicConstructor = true;
100 public string TypeName
107 public string XmlType
121 public string FullTypeName
128 public SchemaTypes SchemaType
135 public bool IsListType
137 get { return SchemaType == SchemaTypes.Array; }
140 public bool IsComplexType
144 return (SchemaType == SchemaTypes.Class ||
145 SchemaType == SchemaTypes.Array ||
146 SchemaType == SchemaTypes.Enum ||
147 SchemaType == SchemaTypes.XmlNode ||
148 SchemaType == SchemaTypes.XmlSerializable ||
153 public bool IsValueType
157 if (type != null) return type.IsValueType;
158 else return (sType == SchemaTypes.Primitive || sType == SchemaTypes.Enum);
162 public TypeData ListItemTypeData
166 if (listItemTypeData == null && type != null)
167 listItemTypeData = TypeTranslator.GetTypeData (ListItemType);
168 return listItemTypeData;
172 public Type ListItemType
177 throw new InvalidOperationException ("Property ListItemType is not supported for custom types");
179 if (listItemType != null) return listItemType;
181 if (SchemaType != SchemaTypes.Array)
182 throw new InvalidOperationException (Type.FullName + " is not a collection");
183 else if (type.IsArray)
184 listItemType = type.GetElementType ();
185 else if (typeof(ICollection).IsAssignableFrom (type))
187 PropertyInfo prop = GetIndexerProperty (type);
189 throw new InvalidOperationException ("You must implement a default accessor on " + type.FullName + " because it inherits from ICollection");
191 return prop.PropertyType;
195 MethodInfo met = type.GetMethod ("Add");
197 throw new InvalidOperationException ("The collection " + type.FullName + " must implement an Add method");
199 ParameterInfo[] pars = met.GetParameters();
200 if (pars.Length != 1)
201 throw new InvalidOperationException ("The Add method of the collection " + type.FullName + " must have only one parameter");
203 return pars[0].ParameterType;
210 public TypeData ListTypeData
214 if (listTypeData != null) return listTypeData;
216 listTypeData = new TypeData (TypeName + "[]",
218 TypeTranslator.GetArrayName(XmlType),
219 SchemaTypes.Array, this);
225 public bool IsXsdType {
226 get { return mappedType == null; }
229 public TypeData MappedType {
231 return mappedType != null ? mappedType : this;
235 public XmlSchemaPatternFacet XmlSchemaPatternFacet {
241 public bool HasPublicConstructor
243 get { return hasPublicConstructor; }
247 public static PropertyInfo GetIndexerProperty (Type collectionType)
249 PropertyInfo[] props = collectionType.GetProperties (BindingFlags.Instance | BindingFlags.Public);
250 foreach (PropertyInfo prop in props)
252 ParameterInfo[] pi = prop.GetIndexParameters ();
253 if (pi != null && pi.Length == 1 && pi[0].ParameterType == typeof(int))