2 // System.Xml.Serialization.XmlReflectionImporter
5 // Tim Coleman (tim@timcoleman.com)
6 // Erik LeBel (eriklebel@yahoo.ca)
7 // Lluis Sanchez Gual (lluis@ximian.com)
9 // Copyright (C) Tim Coleman, 2002
10 // (C) 2003 Erik LeBel
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.Globalization;
36 using System.Reflection;
37 using System.Xml.Schema;
39 namespace System.Xml.Serialization {
40 public class XmlReflectionImporter {
42 string initialDefaultNamespace;
43 XmlAttributeOverrides attributeOverrides;
44 ArrayList includedTypes;
45 ReflectionHelper helper = new ReflectionHelper();
46 int arrayChoiceCount = 1;
47 ArrayList relatedMaps = new ArrayList ();
48 bool allowPrivateTypes = false;
50 static readonly string errSimple = "Cannot serialize object of type '{0}'. Base " +
51 "type '{1}' has simpleContent and can be only extended by adding XmlAttribute " +
52 "elements. Please consider changing XmlText member of the base class to string array";
54 static readonly string errSimple2 = "Cannot serialize object of type '{0}'. " +
55 "Consider changing type of XmlText member '{1}' from '{2}' to string or string array";
59 public XmlReflectionImporter ()
64 public XmlReflectionImporter (string defaultNamespace)
65 : this (null, defaultNamespace)
69 public XmlReflectionImporter (XmlAttributeOverrides attributeOverrides)
70 : this (attributeOverrides, null)
74 public XmlReflectionImporter (XmlAttributeOverrides attributeOverrides, string defaultNamespace)
76 if (defaultNamespace == null)
77 this.initialDefaultNamespace = String.Empty;
79 this.initialDefaultNamespace = defaultNamespace;
81 if (attributeOverrides == null)
82 this.attributeOverrides = new XmlAttributeOverrides();
84 this.attributeOverrides = attributeOverrides;
89 helper = new ReflectionHelper();
94 internal bool AllowPrivateTypes
96 get { return allowPrivateTypes; }
97 set { allowPrivateTypes = value; }
100 #endregion // Constructors
104 public XmlMembersMapping ImportMembersMapping (string elementName,
106 XmlReflectionMember [] members,
107 bool hasWrapperElement)
109 // Reset (); Disabled. See ChangeLog
111 XmlMemberMapping[] mapping = new XmlMemberMapping[members.Length];
112 for (int n=0; n<members.Length; n++)
114 XmlTypeMapMember mapMem = CreateMapMember (null, members[n], ns);
115 mapping[n] = new XmlMemberMapping (members[n].MemberName, ns, mapMem, false);
117 elementName = XmlConvert.EncodeLocalName (elementName);
118 XmlMembersMapping mps = new XmlMembersMapping (elementName, ns, hasWrapperElement, false, mapping);
119 mps.RelatedMaps = relatedMaps;
120 mps.Format = SerializationFormat.Literal;
121 Type[] extraTypes = includedTypes != null ? (Type[])includedTypes.ToArray(typeof(Type)) : null;
122 mps.Source = new MembersSerializationSource (elementName, hasWrapperElement, members, false, true, ns, extraTypes);
123 if (allowPrivateTypes) mps.Source.CanBeGenerated = false;
129 public XmlMembersMapping ImportMembersMapping (string elementName,
131 XmlReflectionMember[] members,
132 bool hasWrapperElement,
135 throw new NotImplementedException ();
139 public XmlMembersMapping ImportMembersMapping (string elementName,
141 XmlReflectionMember[] members,
142 bool hasWrapperElement,
146 throw new NotImplementedException ();
150 public XmlTypeMapping ImportTypeMapping (Type type)
152 return ImportTypeMapping (type, null, null);
155 public XmlTypeMapping ImportTypeMapping (Type type, string defaultNamespace)
157 return ImportTypeMapping (type, null, defaultNamespace);
160 public XmlTypeMapping ImportTypeMapping (Type type, XmlRootAttribute group)
162 return ImportTypeMapping (type, group, null);
165 public XmlTypeMapping ImportTypeMapping (Type type, XmlRootAttribute root, string defaultNamespace)
168 throw new ArgumentNullException ("type");
170 if (type == typeof (void))
171 throw new InvalidOperationException ("Type " + type.Name + " may not be serialized.");
173 if (defaultNamespace == null) defaultNamespace = initialDefaultNamespace;
174 if (defaultNamespace == null) defaultNamespace = string.Empty;
178 switch (TypeTranslator.GetTypeData(type).SchemaType)
180 case SchemaTypes.Class: map = ImportClassMapping (type, root, defaultNamespace); break;
181 case SchemaTypes.Array: map = ImportListMapping (type, root, defaultNamespace, null, 0); break;
182 case SchemaTypes.XmlNode: map = ImportXmlNodeMapping (type, root, defaultNamespace); break;
183 case SchemaTypes.Primitive: map = ImportPrimitiveMapping (type, root, defaultNamespace); break;
184 case SchemaTypes.Enum: map = ImportEnumMapping (type, root, defaultNamespace); break;
185 case SchemaTypes.XmlSerializable: map = ImportXmlSerializableMapping (type, root, defaultNamespace); break;
186 default: throw new NotSupportedException ("Type " + type.FullName + " not supported for XML stialization");
189 map.RelatedMaps = relatedMaps;
190 map.Format = SerializationFormat.Literal;
191 Type[] extraTypes = includedTypes != null ? (Type[])includedTypes.ToArray(typeof(Type)) : null;
192 map.Source = new XmlTypeSerializationSource (type, root, attributeOverrides, defaultNamespace, extraTypes);
193 if (allowPrivateTypes) map.Source.CanBeGenerated = false;
197 XmlTypeMapping CreateTypeMapping (TypeData typeData, XmlRootAttribute root, string defaultXmlType, string defaultNamespace)
199 string rootNamespace = defaultNamespace;
200 string typeNamespace = null;
202 bool includeInSchema = true;
203 XmlAttributes atts = null;
204 bool nullable = true;
206 if (defaultXmlType == null) defaultXmlType = typeData.XmlType;
208 if (!typeData.IsListType)
210 if (attributeOverrides != null)
211 atts = attributeOverrides[typeData.Type];
213 if (atts != null && typeData.SchemaType == SchemaTypes.Primitive)
214 throw new InvalidOperationException ("XmlRoot and XmlType attributes may not be specified for the type " + typeData.FullTypeName);
218 atts = new XmlAttributes (typeData.Type);
220 if (atts.XmlRoot != null && root == null)
223 if (atts.XmlType != null)
225 if (atts.XmlType.Namespace != null && typeData.SchemaType != SchemaTypes.Enum)
226 typeNamespace = atts.XmlType.Namespace;
228 if (atts.XmlType.TypeName != null && atts.XmlType.TypeName != string.Empty)
229 defaultXmlType = XmlConvert.EncodeLocalName (atts.XmlType.TypeName);
231 includeInSchema = atts.XmlType.IncludeInSchema;
234 elementName = defaultXmlType;
238 if (root.ElementName.Length != 0)
239 elementName = XmlConvert.EncodeLocalName(root.ElementName);
240 if (root.Namespace != null)
241 rootNamespace = root.Namespace;
242 nullable = root.IsNullable;
245 if (rootNamespace == null) rootNamespace = "";
246 if (typeNamespace == null) typeNamespace = rootNamespace;
249 if (typeData.SchemaType == SchemaTypes.XmlSerializable)
250 map = new XmlSerializableMapping (elementName, rootNamespace, typeData, defaultXmlType, typeNamespace);
252 map = new XmlTypeMapping (elementName, rootNamespace, typeData, defaultXmlType, typeNamespace);
254 map.IncludeInSchema = includeInSchema;
255 map.IsNullable = nullable;
256 relatedMaps.Add (map);
261 XmlTypeMapping ImportClassMapping (Type type, XmlRootAttribute root, string defaultNamespace)
263 TypeData typeData = TypeTranslator.GetTypeData (type);
264 XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
265 if (map != null) return map;
267 if (!allowPrivateTypes)
268 ReflectionHelper.CheckSerializableType (type, false);
270 map = CreateTypeMapping (typeData, root, null, defaultNamespace);
271 helper.RegisterClrType (map, type, map.XmlTypeNamespace);
272 helper.RegisterSchemaType (map, map.XmlType, map.XmlTypeNamespace);
276 ClassMap classMap = new ClassMap ();
277 map.ObjectMap = classMap;
281 ICollection members = GetReflectionMembers (type);
282 foreach (XmlReflectionMember rmember in members)
284 string ns = map.XmlTypeNamespace;
285 if (rmember.XmlAttributes.XmlIgnore) continue;
286 if (rmember.DeclaringType != null && rmember.DeclaringType != type) {
287 XmlTypeMapping bmap = ImportClassMapping (rmember.DeclaringType, root, defaultNamespace);
288 ns = bmap.XmlTypeNamespace;
291 XmlTypeMapMember mem = CreateMapMember (type, rmember, ns);
292 mem.CheckOptionalValueType (type);
293 classMap.AddMember (mem);
296 // catch (Exception ex) {
297 // throw helper.CreateError (map, ex.Message);
300 // Import extra classes
302 if (type == typeof (object) && includedTypes != null)
304 foreach (Type intype in includedTypes)
305 map.DerivedTypes.Add (ImportTypeMapping (intype, defaultNamespace));
308 // Register inheritance relations
310 if (type.BaseType != null)
312 XmlTypeMapping bmap = ImportClassMapping (type.BaseType, root, defaultNamespace);
313 ClassMap cbmap = bmap.ObjectMap as ClassMap;
315 if (type.BaseType != typeof (object)) {
317 if (!cbmap.HasSimpleContent)
318 classMap.SetCanBeSimpleType (false);
321 // At this point, derived classes of this map must be already registered
323 RegisterDerivedMap (bmap, map);
325 if (cbmap.HasSimpleContent && classMap.ElementMembers != null && classMap.ElementMembers.Count != 1)
326 throw new InvalidOperationException (String.Format (errSimple, map.TypeData.TypeName, map.BaseMap.TypeData.TypeName));
329 ImportIncludedTypes (type, defaultNamespace);
331 if (classMap.XmlTextCollector != null && !classMap.HasSimpleContent)
333 XmlTypeMapMember mem = classMap.XmlTextCollector;
334 if (mem.TypeData.Type != typeof(string) &&
335 mem.TypeData.Type != typeof(string[]) &&
336 mem.TypeData.Type != typeof(object[]) &&
337 mem.TypeData.Type != typeof(XmlNode[]))
339 throw new InvalidOperationException (String.Format (errSimple2, map.TypeData.TypeName, mem.Name, mem.TypeData.TypeName));
345 void RegisterDerivedMap (XmlTypeMapping map, XmlTypeMapping derivedMap)
347 map.DerivedTypes.Add (derivedMap);
348 map.DerivedTypes.AddRange (derivedMap.DerivedTypes);
350 if (map.BaseMap != null)
351 RegisterDerivedMap (map.BaseMap, derivedMap);
353 XmlTypeMapping obmap = ImportTypeMapping (typeof(object));
355 obmap.DerivedTypes.Add (derivedMap);
359 string GetTypeNamespace (TypeData typeData, XmlRootAttribute root, string defaultNamespace)
361 string typeNamespace = null;
363 XmlAttributes atts = null;
364 if (!typeData.IsListType)
366 if (attributeOverrides != null)
367 atts = attributeOverrides[typeData.Type];
371 atts = new XmlAttributes (typeData.Type);
373 if (atts.XmlType != null)
375 if (atts.XmlType.Namespace != null && atts.XmlType.Namespace.Length != 0 && typeData.SchemaType != SchemaTypes.Enum)
376 typeNamespace = atts.XmlType.Namespace;
379 if (typeNamespace != null && typeNamespace.Length != 0) return typeNamespace;
381 if (atts.XmlRoot != null && root == null)
386 if (root.Namespace != null && root.Namespace.Length != 0)
387 return root.Namespace;
390 if (defaultNamespace == null) return "";
391 else return defaultNamespace;
394 XmlTypeMapping ImportListMapping (Type type, XmlRootAttribute root, string defaultNamespace, XmlAttributes atts, int nestingLevel)
396 TypeData typeData = TypeTranslator.GetTypeData (type);
397 ListMap obmap = new ListMap ();
399 if (!allowPrivateTypes)
400 ReflectionHelper.CheckSerializableType (type, true);
402 if (atts == null) atts = new XmlAttributes();
403 Type itemType = typeData.ListItemType;
405 // warning: byte[][] should not be considered multiarray
406 bool isMultiArray = (type.IsArray && (TypeTranslator.GetTypeData(itemType).SchemaType == SchemaTypes.Array) && itemType.IsArray);
408 XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
410 foreach (XmlArrayItemAttribute att in atts.XmlArrayItems)
412 if (att.NestingLevel != nestingLevel) continue;
413 Type elemType = (att.Type != null) ? att.Type : itemType;
414 XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData(elemType, att.DataType));
415 elem.Namespace = att.Namespace != null ? att.Namespace : defaultNamespace;
416 if (elem.Namespace == null) elem.Namespace = "";
417 elem.Form = att.Form;
418 elem.IsNullable = att.IsNullable && CanBeNull (elem.TypeData);
419 elem.NestingLevel = att.NestingLevel;
422 elem.MappedType = ImportListMapping (elemType, null, elem.Namespace, atts, nestingLevel + 1);
423 } else if (elem.TypeData.IsComplexType) {
424 elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
427 if (att.ElementName.Length != 0) {
428 elem.ElementName = XmlConvert.EncodeLocalName (att.ElementName);
429 } else if (elem.MappedType != null) {
430 elem.ElementName = elem.MappedType.ElementName;
432 elem.ElementName = TypeTranslator.GetTypeData (elemType).XmlType;
440 XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData (itemType));
442 elem.MappedType = ImportListMapping (itemType, null, defaultNamespace, atts, nestingLevel + 1);
443 else if (elem.TypeData.IsComplexType)
444 elem.MappedType = ImportTypeMapping (itemType, null, defaultNamespace);
446 if (elem.MappedType != null) {
447 elem.ElementName = elem.MappedType.XmlType;
449 elem.ElementName = TypeTranslator.GetTypeData (itemType).XmlType;
452 elem.Namespace = (defaultNamespace != null) ? defaultNamespace : "";
453 elem.IsNullable = CanBeNull (elem.TypeData);
457 obmap.ItemInfo = list;
459 // If there can be different element names (types) in the array, then its name cannot
460 // be "ArrayOfXXX" it must be something like ArrayOfChoiceNNN
463 if (list.Count > 1) {
464 baseName = "ArrayOfChoice" + (arrayChoiceCount++);
466 XmlTypeMapElementInfo elem = ((XmlTypeMapElementInfo) list[0]);
467 if (elem.MappedType != null) {
468 baseName = TypeTranslator.GetArrayName (elem.MappedType.XmlType);
470 baseName = TypeTranslator.GetArrayName (elem.ElementName);
474 // Avoid name colisions
477 string name = baseName;
480 XmlTypeMapping foundMap = helper.GetRegisteredSchemaType (name, defaultNamespace);
481 if (foundMap == null) nameCount = -1;
482 else if (obmap.Equals (foundMap.ObjectMap) && typeData.Type == foundMap.TypeData.Type) return foundMap;
483 else name = baseName + (nameCount++);
485 while (nameCount != -1);
487 XmlTypeMapping map = CreateTypeMapping (typeData, root, name, defaultNamespace);
488 map.ObjectMap = obmap;
490 // Register any of the including types as a derived class of object
491 XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes (typeof (XmlIncludeAttribute), false);
493 XmlTypeMapping objectMapping = ImportTypeMapping (typeof(object));
494 for (int i = 0; i < includes.Length; i++)
496 Type includedType = includes[i].Type;
497 objectMapping.DerivedTypes.Add(ImportTypeMapping (includedType, null, defaultNamespace));
500 // Register this map as a derived class of object
502 helper.RegisterSchemaType (map, name, defaultNamespace);
503 ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
508 XmlTypeMapping ImportXmlNodeMapping (Type type, XmlRootAttribute root, string defaultNamespace)
510 XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (TypeTranslator.GetTypeData (type), root, defaultNamespace));
511 if (map != null) return map;
513 map = CreateTypeMapping (TypeTranslator.GetTypeData (type), root, null, defaultNamespace);
514 helper.RegisterClrType (map, type, map.XmlTypeNamespace);
516 if (type.BaseType != null)
518 XmlTypeMapping bmap = ImportTypeMapping (type.BaseType, root, defaultNamespace);
519 if (type.BaseType != typeof (object))
522 RegisterDerivedMap (bmap, map);
528 XmlTypeMapping ImportPrimitiveMapping (Type type, XmlRootAttribute root, string defaultNamespace)
530 TypeData typeData = TypeTranslator.GetTypeData (type);
531 XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
532 if (map != null) return map;
533 map = CreateTypeMapping (typeData, root, null, defaultNamespace);
534 helper.RegisterClrType (map, type, map.XmlTypeNamespace);
538 XmlTypeMapping ImportEnumMapping (Type type, XmlRootAttribute root, string defaultNamespace)
540 TypeData typeData = TypeTranslator.GetTypeData (type);
541 XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
542 if (map != null) return map;
544 if (!allowPrivateTypes)
545 ReflectionHelper.CheckSerializableType (type, false);
547 map = CreateTypeMapping (typeData, root, null, defaultNamespace);
548 helper.RegisterClrType (map, type, map.XmlTypeNamespace);
550 string [] names = Enum.GetNames (type);
551 ArrayList members = new ArrayList();
552 foreach (string name in names)
554 MemberInfo[] mem = type.GetMember (name);
555 string xmlName = null;
556 object[] atts = mem[0].GetCustomAttributes (typeof(XmlIgnoreAttribute), false);
557 if (atts.Length > 0) continue;
558 atts = mem[0].GetCustomAttributes (typeof(XmlEnumAttribute), false);
559 if (atts.Length > 0) xmlName = ((XmlEnumAttribute)atts[0]).Name;
560 if (xmlName == null) xmlName = name;
561 members.Add (new EnumMap.EnumMapMember (xmlName, name));
564 bool isFlags = type.GetCustomAttributes (typeof(FlagsAttribute),false).Length > 0;
565 map.ObjectMap = new EnumMap ((EnumMap.EnumMapMember[])members.ToArray (typeof(EnumMap.EnumMapMember)), isFlags);
566 ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
570 XmlTypeMapping ImportXmlSerializableMapping (Type type, XmlRootAttribute root, string defaultNamespace)
572 TypeData typeData = TypeTranslator.GetTypeData (type);
573 XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
574 if (map != null) return map;
576 if (!allowPrivateTypes)
577 ReflectionHelper.CheckSerializableType (type, false);
579 map = CreateTypeMapping (typeData, root, null, defaultNamespace);
580 helper.RegisterClrType (map, type, map.XmlTypeNamespace);
584 void ImportIncludedTypes (Type type, string defaultNamespace)
586 XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes (typeof (XmlIncludeAttribute), false);
587 for (int n=0; n<includes.Length; n++)
589 Type includedType = includes[n].Type;
590 ImportTypeMapping (includedType, null, defaultNamespace);
594 ICollection GetReflectionMembers (Type type)
596 // First we want to find the inheritance hierarchy in reverse order.
597 Type currentType = type;
598 ArrayList typeList = new ArrayList();
599 typeList.Add(currentType);
600 while (currentType != typeof(object))
602 currentType = currentType.BaseType; // Read the base type.
603 typeList.Insert(0, currentType); // Insert at 0 to reverse the order.
606 // Read all Fields via reflection.
607 ArrayList fieldList = new ArrayList();
608 FieldInfo[] tfields = type.GetFields (BindingFlags.Instance | BindingFlags.Public);
610 // This statement ensures fields are ordered starting from the base type.
611 for (int ti=0; ti<typeList.Count; ti++) {
612 for (int i=0; i<tfields.Length; i++) {
613 FieldInfo field = tfields[i];
614 if (field.DeclaringType == typeList[ti])
615 fieldList.Add (field);
620 int currentIndex = 0;
621 foreach (FieldInfo field in tfields)
623 // This statement ensures fields are ordered starting from the base type.
624 if (currentType != field.DeclaringType)
626 currentType = field.DeclaringType;
629 fieldList.Insert(currentIndex++, field);
632 // Read all Properties via reflection.
633 ArrayList propList = new ArrayList();
634 PropertyInfo[] tprops = type.GetProperties (BindingFlags.Instance | BindingFlags.Public);
636 // This statement ensures properties are ordered starting from the base type.
637 for (int ti=0; ti<typeList.Count; ti++) {
638 for (int i=0; i<tprops.Length; i++) {
639 PropertyInfo prop = tprops[i];
640 if (!prop.CanRead) continue;
641 if (prop.GetIndexParameters().Length > 0) continue;
642 if (prop.DeclaringType == typeList[ti])
649 foreach (PropertyInfo prop in tprops)
651 // This statement ensures properties are ordered starting from the base type.
652 if (currentType != prop.DeclaringType)
654 currentType = prop.DeclaringType;
657 if (!prop.CanRead) continue;
658 if (prop.GetIndexParameters().Length > 0) continue;
659 propList.Insert(currentIndex++, prop);
662 ArrayList members = new ArrayList();
665 // We now step through the type hierarchy from the base (object) through
666 // to the supplied class, as each step outputting all Fields, and then
667 // all Properties. This is the exact same ordering as .NET 1.0/1.1.
668 foreach (Type t in typeList)
670 // Add any fields matching the current DeclaringType.
671 while (fieldIndex < fieldList.Count)
673 FieldInfo field = (FieldInfo)fieldList[fieldIndex];
674 if (field.DeclaringType==t)
677 XmlAttributes atts = attributeOverrides[type, field.Name];
678 if (atts == null) atts = new XmlAttributes (field);
679 if (atts.XmlIgnore) continue;
680 XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
681 member.DeclaringType = field.DeclaringType;
687 // Add any properties matching the current DeclaringType.
688 while (propIndex < propList.Count)
690 PropertyInfo prop = (PropertyInfo)propList[propIndex];
691 if (prop.DeclaringType==t)
694 XmlAttributes atts = attributeOverrides[type, prop.Name];
695 if (atts == null) atts = new XmlAttributes (prop);
696 if (atts.XmlIgnore) continue;
697 if (!prop.CanWrite && (TypeTranslator.GetTypeData (prop.PropertyType).SchemaType != SchemaTypes.Array || prop.PropertyType.IsArray)) continue;
698 XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
699 member.DeclaringType = prop.DeclaringType;
708 private XmlTypeMapMember CreateMapMember (Type declaringType, XmlReflectionMember rmember, string defaultNamespace)
710 XmlTypeMapMember mapMember;
711 XmlAttributes atts = rmember.XmlAttributes;
712 TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType);
714 if (atts.XmlAnyAttribute != null)
716 if ( (rmember.MemberType.FullName == "System.Xml.XmlAttribute[]") ||
717 (rmember.MemberType.FullName == "System.Xml.XmlNode[]") )
719 mapMember = new XmlTypeMapMemberAnyAttribute();
722 throw new InvalidOperationException ("XmlAnyAttributeAttribute can only be applied to members of type XmlAttribute[] or XmlNode[]");
724 else if (atts.XmlAnyElements != null && atts.XmlAnyElements.Count > 0)
726 if ( (rmember.MemberType.FullName == "System.Xml.XmlElement[]") ||
727 (rmember.MemberType.FullName == "System.Xml.XmlNode[]") ||
728 (rmember.MemberType.FullName == "System.Xml.XmlElement"))
730 XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement();
731 member.ElementInfo = ImportAnyElementInfo (defaultNamespace, rmember, member, atts);
735 throw new InvalidOperationException ("XmlAnyElementAttribute can only be applied to members of type XmlElement, XmlElement[] or XmlNode[]");
739 XmlTypeMapMemberNamespaces mapNamespaces = new XmlTypeMapMemberNamespaces ();
740 mapMember = mapNamespaces;
742 else if (atts.XmlAttribute != null)
746 if (atts.XmlElements != null && atts.XmlElements.Count > 0)
747 throw new Exception ("XmlAttributeAttribute and XmlElementAttribute cannot be applied to the same member");
749 XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute ();
750 if (atts.XmlAttribute.AttributeName.Length == 0)
751 mapAttribute.AttributeName = rmember.MemberName;
753 mapAttribute.AttributeName = atts.XmlAttribute.AttributeName;
755 mapAttribute.AttributeName = XmlConvert.EncodeLocalName (mapAttribute.AttributeName);
757 if (typeData.IsComplexType)
758 mapAttribute.MappedType = ImportTypeMapping (typeData.Type, null, mapAttribute.Namespace);
760 if (atts.XmlAttribute.Namespace != null && atts.XmlAttribute.Namespace != defaultNamespace)
762 if (atts.XmlAttribute.Form == XmlSchemaForm.Unqualified)
763 throw new InvalidOperationException ("The Form property may not be 'Unqualified' when an explicit Namespace property is present");
764 mapAttribute.Form = XmlSchemaForm.Qualified;
765 mapAttribute.Namespace = atts.XmlAttribute.Namespace;
769 mapAttribute.Form = atts.XmlAttribute.Form;
770 if (atts.XmlAttribute.Form == XmlSchemaForm.Qualified)
771 mapAttribute.Namespace = defaultNamespace;
773 mapAttribute.Namespace = "";
776 typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.XmlAttribute.DataType);
777 mapMember = mapAttribute;
779 else if (typeData.SchemaType == SchemaTypes.Array)
781 // If the member has a single XmlElementAttribute and the type is the type of the member,
782 // then it is not a flat list
784 if (atts.XmlElements.Count > 1 ||
785 (atts.XmlElements.Count == 1 && atts.XmlElements[0].Type != typeData.Type) ||
786 (atts.XmlText != null))
790 // TODO: check that it does not have XmlArrayAttribute
791 XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
792 member.ListMap = new ListMap ();
793 member.ListMap.ItemInfo = ImportElementInfo (declaringType, XmlConvert.EncodeLocalName (rmember.MemberName), defaultNamespace, typeData.ListItemType, member, atts);
794 member.ElementInfo = member.ListMap.ItemInfo;
795 member.ListMap.ChoiceMember = member.ChoiceMember;
802 XmlTypeMapMemberList member = new XmlTypeMapMemberList ();
804 // Creates an ElementInfo that identifies the array instance.
805 member.ElementInfo = new XmlTypeMapElementInfoList();
806 XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, typeData);
807 elem.ElementName = XmlConvert.EncodeLocalName((atts.XmlArray != null && atts.XmlArray.ElementName.Length != 0) ? atts.XmlArray.ElementName : rmember.MemberName);
808 elem.Namespace = (atts.XmlArray != null && atts.XmlArray.Namespace != null) ? atts.XmlArray.Namespace : defaultNamespace;
809 elem.MappedType = ImportListMapping (rmember.MemberType, null, elem.Namespace, atts, 0);
810 elem.IsNullable = (atts.XmlArray != null) ? atts.XmlArray.IsNullable : false;
811 elem.Form = (atts.XmlArray != null) ? atts.XmlArray.Form : XmlSchemaForm.Qualified;
813 member.ElementInfo.Add (elem);
821 XmlTypeMapMemberElement member = new XmlTypeMapMemberElement ();
822 member.ElementInfo = ImportElementInfo (declaringType, XmlConvert.EncodeLocalName(rmember.MemberName), defaultNamespace, rmember.MemberType, member, atts);
826 mapMember.DefaultValue = atts.XmlDefaultValue;
827 mapMember.TypeData = typeData;
828 mapMember.Name = rmember.MemberName;
829 mapMember.IsReturnValue = rmember.IsReturnValue;
833 XmlTypeMapElementInfoList ImportElementInfo (Type cls, string defaultName, string defaultNamespace, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
835 EnumMap choiceEnumMap = null;
836 Type choiceEnumType = null;
838 XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
839 ImportTextElementInfo (list, defaultType, member, atts);
841 if (atts.XmlChoiceIdentifier != null) {
843 throw new InvalidOperationException ("XmlChoiceIdentifierAttribute not supported in this context.");
845 member.ChoiceMember = atts.XmlChoiceIdentifier.MemberName;
846 MemberInfo[] mems = cls.GetMember (member.ChoiceMember, BindingFlags.Instance|BindingFlags.Public);
848 if (mems.Length == 0)
849 throw new InvalidOperationException ("Choice member '" + member.ChoiceMember + "' not found in class '" + cls);
851 if (mems[0] is PropertyInfo) {
852 PropertyInfo pi = (PropertyInfo)mems[0];
853 if (!pi.CanWrite || !pi.CanRead)
854 throw new InvalidOperationException ("Choice property '" + member.ChoiceMember + "' must be read/write.");
855 choiceEnumType = pi.PropertyType;
857 else choiceEnumType = ((FieldInfo)mems[0]).FieldType;
859 member.ChoiceTypeData = TypeTranslator.GetTypeData (choiceEnumType);
861 if (choiceEnumType.IsArray)
862 choiceEnumType = choiceEnumType.GetElementType ();
864 choiceEnumMap = ImportTypeMapping (choiceEnumType).ObjectMap as EnumMap;
865 if (choiceEnumMap == null)
866 throw new InvalidOperationException ("The member '" + mems[0].Name + "' is not a valid target for XmlChoiceIdentifierAttribute.");
869 if (atts.XmlElements.Count == 0 && list.Count == 0)
871 XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType));
872 elem.ElementName = defaultName;
873 elem.Namespace = defaultNamespace;
874 if (elem.TypeData.IsComplexType)
875 elem.MappedType = ImportTypeMapping (defaultType, null, defaultNamespace);
879 bool multiType = (atts.XmlElements.Count > 1);
880 foreach (XmlElementAttribute att in atts.XmlElements)
882 Type elemType = (att.Type != null) ? att.Type : defaultType;
883 XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(elemType, att.DataType));
884 elem.Namespace = (att.Namespace != null) ? att.Namespace : defaultNamespace;
885 elem.Form = att.Form;
886 elem.IsNullable = att.IsNullable;
888 if (elem.IsNullable && elem.TypeData.IsValueType)
889 throw new InvalidOperationException ("IsNullable may not be 'true' for value type " + elem.TypeData.FullTypeName + " in member '" + defaultName + "'");
891 if (elem.TypeData.IsComplexType)
893 if (att.DataType.Length != 0) throw new InvalidOperationException (
894 string.Format(CultureInfo.InvariantCulture, "'{0}' is "
895 + "an invalid value for '{1}.{2}' of type '{3}'. "
896 + "The property may only be specified for primitive types.",
897 att.DataType, cls.FullName, defaultName,
898 elem.TypeData.FullTypeName));
899 elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
902 if (att.ElementName.Length != 0) {
903 elem.ElementName = XmlConvert.EncodeLocalName(att.ElementName);
904 } else if (multiType) {
905 if (elem.MappedType != null) {
906 elem.ElementName = elem.MappedType.ElementName;
908 elem.ElementName = TypeTranslator.GetTypeData (elemType).XmlType;
911 elem.ElementName = defaultName;
914 if (choiceEnumMap != null) {
915 string cname = choiceEnumMap.GetEnumName (elem.ElementName);
916 if (cname == null) throw new InvalidOperationException ("The '" + choiceEnumType + "' enumeration does not have a value for the element '" + elem.ElementName + "'");
917 elem.ChoiceValue = Enum.Parse (choiceEnumType, cname);
925 XmlTypeMapElementInfoList ImportAnyElementInfo (string defaultNamespace, XmlReflectionMember rmember, XmlTypeMapMemberElement member, XmlAttributes atts)
927 XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
929 ImportTextElementInfo (list, rmember.MemberType, member, atts);
931 foreach (XmlAnyElementAttribute att in atts.XmlAnyElements)
933 XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(typeof(XmlElement)));
934 if (att.Name.Length != 0)
936 elem.ElementName = XmlConvert.EncodeLocalName(att.Name);
937 elem.Namespace = (att.Namespace != null) ? att.Namespace : "";
941 elem.IsUnnamedAnyElement = true;
942 elem.Namespace = defaultNamespace;
943 if (att.Namespace != null)
944 throw new InvalidOperationException ("The element " + rmember.MemberName + " has been attributed with an XmlAnyElementAttribute and a namespace '" + att.Namespace + "', but no name. When a namespace is supplied, a name is also required. Supply a name or remove the namespace.");
951 void ImportTextElementInfo (XmlTypeMapElementInfoList list, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
953 if (atts.XmlText != null)
955 member.IsXmlTextCollector = true;
956 if (atts.XmlText.Type != null) defaultType = atts.XmlText.Type;
957 if (defaultType == typeof(XmlNode)) defaultType = typeof(XmlText); // Nodes must be text nodes
959 XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType, atts.XmlText.DataType));
961 if (elem.TypeData.SchemaType != SchemaTypes.Primitive &&
962 elem.TypeData.SchemaType != SchemaTypes.Enum &&
963 elem.TypeData.SchemaType != SchemaTypes.XmlNode &&
964 !(elem.TypeData.SchemaType == SchemaTypes.Array && elem.TypeData.ListItemTypeData.SchemaType == SchemaTypes.XmlNode)
966 throw new InvalidOperationException ("XmlText cannot be used to encode complex types");
968 elem.IsTextElement = true;
969 elem.WrappedElement = false;
974 bool CanBeNull (TypeData type)
976 return (type.SchemaType != SchemaTypes.Primitive || type.Type == typeof (string));
979 public void IncludeType (Type type)
982 throw new ArgumentNullException ("type");
984 if (includedTypes == null) includedTypes = new ArrayList ();
985 if (!includedTypes.Contains (type))
986 includedTypes.Add (type);
988 if (relatedMaps.Count > 0) {
989 foreach (XmlTypeMapping map in (ArrayList) relatedMaps.Clone ()) {
990 if (map.TypeData.Type == typeof(object))
991 map.DerivedTypes.Add (ImportTypeMapping (type));
996 public void IncludeTypes (ICustomAttributeProvider provider)
998 object[] ats = provider.GetCustomAttributes (typeof(XmlIncludeAttribute), true);
1000 foreach (XmlIncludeAttribute at in ats)
1001 IncludeType (at.Type);
1004 #endregion // Methods