XmlReflectionImporter.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. //
  2. // System.Xml.Serialization.XmlReflectionImporter
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Erik LeBel ([email protected])
  7. // Lluis Sanchez Gual ([email protected])
  8. //
  9. // Copyright (C) Tim Coleman, 2002
  10. // (C) 2003 Erik LeBel
  11. //
  12. using System.Reflection;
  13. using System.Collections;
  14. using System.Xml.Schema;
  15. namespace System.Xml.Serialization {
  16. public class XmlReflectionImporter {
  17. string initialDefaultNamespace;
  18. XmlAttributeOverrides attributeOverrides;
  19. ArrayList includedTypes;
  20. ReflectionHelper helper = new ReflectionHelper();
  21. int arrayChoiceCount = 1;
  22. ArrayList relatedMaps = new ArrayList ();
  23. bool allowPrivateTypes = false;
  24. static readonly string errSimple = "Cannot serialize object of type '{0}'. Base " +
  25. "type '{1}' has simpleContent and can be only extended by adding XmlAttribute " +
  26. "elements. Please consider changing XmlText member of the base class to string array";
  27. static readonly string errSimple2 = "Cannot serialize object of type '{0}'. " +
  28. "Consider changing type of XmlText member '{1}' from '{2}' to string or string array";
  29. #region Constructors
  30. public XmlReflectionImporter ()
  31. : this (null, null)
  32. {
  33. }
  34. public XmlReflectionImporter (string defaultNamespace)
  35. : this (null, defaultNamespace)
  36. {
  37. }
  38. public XmlReflectionImporter (XmlAttributeOverrides attributeOverrides)
  39. : this (attributeOverrides, null)
  40. {
  41. }
  42. public XmlReflectionImporter (XmlAttributeOverrides attributeOverrides, string defaultNamespace)
  43. {
  44. if (defaultNamespace == null)
  45. this.initialDefaultNamespace = String.Empty;
  46. else
  47. this.initialDefaultNamespace = defaultNamespace;
  48. if (attributeOverrides == null)
  49. this.attributeOverrides = new XmlAttributeOverrides();
  50. else
  51. this.attributeOverrides = attributeOverrides;
  52. }
  53. void Reset ()
  54. {
  55. helper = new ReflectionHelper();
  56. arrayChoiceCount = 1;
  57. }
  58. internal bool AllowPrivateTypes
  59. {
  60. get { return allowPrivateTypes; }
  61. set { allowPrivateTypes = value; }
  62. }
  63. #endregion // Constructors
  64. #region Methods
  65. public XmlMembersMapping ImportMembersMapping (string elementName,
  66. string ns,
  67. XmlReflectionMember [] members,
  68. bool hasWrapperElement)
  69. {
  70. Reset ();
  71. XmlMemberMapping[] mapping = new XmlMemberMapping[members.Length];
  72. for (int n=0; n<members.Length; n++)
  73. {
  74. XmlTypeMapMember mapMem = CreateMapMember (members[n], ns);
  75. mapping[n] = new XmlMemberMapping (members[n].MemberName, ns, mapMem, false);
  76. }
  77. XmlMembersMapping mps = new XmlMembersMapping (elementName, ns, hasWrapperElement, false, mapping);
  78. mps.RelatedMaps = relatedMaps;
  79. mps.Format = SerializationFormat.Literal;
  80. mps.Source = new MembersSerializationSource (elementName, hasWrapperElement, members, false, true, ns, includedTypes);
  81. if (allowPrivateTypes) mps.Source.CanBeGenerated = false;
  82. return mps;
  83. }
  84. public XmlTypeMapping ImportTypeMapping (Type type)
  85. {
  86. return ImportTypeMapping (type, null, null);
  87. }
  88. public XmlTypeMapping ImportTypeMapping (Type type, string defaultNamespace)
  89. {
  90. return ImportTypeMapping (type, null, defaultNamespace);
  91. }
  92. public XmlTypeMapping ImportTypeMapping (Type type, XmlRootAttribute group)
  93. {
  94. return ImportTypeMapping (type, group, null);
  95. }
  96. public XmlTypeMapping ImportTypeMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  97. {
  98. if (type == null)
  99. throw new ArgumentNullException ("type");
  100. if (type == typeof (void))
  101. throw new InvalidOperationException ("Type " + type.Name + " may not be serialized.");
  102. if (defaultNamespace == null) defaultNamespace = initialDefaultNamespace;
  103. if (defaultNamespace == null) defaultNamespace = string.Empty;
  104. XmlTypeMapping map;
  105. switch (TypeTranslator.GetTypeData(type).SchemaType)
  106. {
  107. case SchemaTypes.Class: map = ImportClassMapping (type, root, defaultNamespace); break;
  108. case SchemaTypes.Array: map = ImportListMapping (type, root, defaultNamespace, null, 0); break;
  109. case SchemaTypes.XmlNode: map = ImportXmlNodeMapping (type, root, defaultNamespace); break;
  110. case SchemaTypes.Primitive: map = ImportPrimitiveMapping (type, root, defaultNamespace); break;
  111. case SchemaTypes.Enum: map = ImportEnumMapping (type, root, defaultNamespace); break;
  112. case SchemaTypes.XmlSerializable: map = ImportXmlSerializableMapping (type, root, defaultNamespace); break;
  113. default: throw new NotSupportedException ("Type " + type.FullName + " not supported for XML stialization");
  114. }
  115. map.RelatedMaps = relatedMaps;
  116. map.Format = SerializationFormat.Literal;
  117. map.Source = new XmlTypeSerializationSource (type, root, attributeOverrides, defaultNamespace, includedTypes);
  118. if (allowPrivateTypes) map.Source.CanBeGenerated = false;
  119. return map;
  120. }
  121. XmlTypeMapping CreateTypeMapping (TypeData typeData, XmlRootAttribute root, string defaultXmlType, string defaultNamespace)
  122. {
  123. string rootNamespace = defaultNamespace;
  124. string typeNamespace = null;
  125. string elementName;
  126. bool includeInSchema = true;
  127. XmlAttributes atts = null;
  128. if (defaultXmlType == null) defaultXmlType = typeData.XmlType;
  129. if (!typeData.IsListType)
  130. {
  131. if (attributeOverrides != null)
  132. atts = attributeOverrides[typeData.Type];
  133. if (atts != null && typeData.SchemaType == SchemaTypes.Primitive)
  134. throw new InvalidOperationException ("XmlRoot and XmlType attributes may not be specified for the type " + typeData.FullTypeName);
  135. }
  136. if (atts == null)
  137. atts = new XmlAttributes (typeData.Type);
  138. if (atts.XmlRoot != null && root == null)
  139. root = atts.XmlRoot;
  140. if (atts.XmlType != null)
  141. {
  142. if (atts.XmlType.Namespace != null && atts.XmlType.Namespace != string.Empty && typeData.SchemaType != SchemaTypes.Enum)
  143. typeNamespace = atts.XmlType.Namespace;
  144. if (atts.XmlType.TypeName != null && atts.XmlType.TypeName != string.Empty)
  145. defaultXmlType = atts.XmlType.TypeName;
  146. includeInSchema = atts.XmlType.IncludeInSchema;
  147. }
  148. elementName = defaultXmlType;
  149. if (root != null)
  150. {
  151. if (root.ElementName != null && root.ElementName != String.Empty)
  152. elementName = root.ElementName;
  153. if (root.Namespace != null && root.Namespace != String.Empty)
  154. rootNamespace = root.Namespace;
  155. }
  156. if (rootNamespace == null) rootNamespace = "";
  157. if (typeNamespace == null || typeNamespace.Length == 0) typeNamespace = rootNamespace;
  158. XmlTypeMapping map = new XmlTypeMapping (elementName, rootNamespace, typeData, defaultXmlType, typeNamespace);
  159. map.IncludeInSchema = includeInSchema;
  160. relatedMaps.Add (map);
  161. return map;
  162. }
  163. XmlTypeMapping ImportClassMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  164. {
  165. TypeData typeData = TypeTranslator.GetTypeData (type);
  166. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
  167. if (map != null) return map;
  168. if (!allowPrivateTypes)
  169. ReflectionHelper.CheckSerializableType (type);
  170. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  171. helper.RegisterClrType (map, type, map.XmlTypeNamespace);
  172. helper.RegisterSchemaType (map, map.XmlType, map.XmlTypeNamespace);
  173. // Import members
  174. ClassMap classMap = new ClassMap ();
  175. map.ObjectMap = classMap;
  176. // try
  177. // {
  178. ICollection members = GetReflectionMembers (type);
  179. foreach (XmlReflectionMember rmember in members)
  180. {
  181. if (rmember.XmlAttributes.XmlIgnore) continue;
  182. XmlTypeMapMember mem = CreateMapMember (rmember, map.XmlTypeNamespace);
  183. mem.CheckOptionalValueType (type);
  184. classMap.AddMember (mem);
  185. }
  186. // }
  187. // catch (Exception ex) {
  188. // throw helper.CreateError (map, ex.Message);
  189. // }
  190. ImportIncludedTypes (type, defaultNamespace);
  191. // Import extra classes
  192. if (type == typeof (object) && includedTypes != null)
  193. {
  194. foreach (Type intype in includedTypes)
  195. map.DerivedTypes.Add (ImportTypeMapping (intype, defaultNamespace));
  196. }
  197. // Register inheritance relations
  198. if (type.BaseType != null)
  199. {
  200. XmlTypeMapping bmap = ImportClassMapping (type.BaseType, root, defaultNamespace);
  201. if (type.BaseType != typeof (object)) {
  202. map.BaseMap = bmap;
  203. classMap.SetCanBeSimpleType (false);
  204. }
  205. // At this point, derived classes of this map must be already registered
  206. RegisterDerivedMap (bmap, map);
  207. if (((ClassMap)bmap.ObjectMap).HasSimpleContent && classMap.ElementMembers != null && classMap.ElementMembers.Count != 1)
  208. throw new InvalidOperationException (String.Format (errSimple, map.TypeData.TypeName, map.BaseMap.TypeData.TypeName));
  209. }
  210. if (classMap.XmlTextCollector != null && !classMap.HasSimpleContent)
  211. {
  212. XmlTypeMapMember mem = classMap.XmlTextCollector;
  213. if (mem.TypeData.Type != typeof(string) &&
  214. mem.TypeData.Type != typeof(string[]) &&
  215. mem.TypeData.Type != typeof(object[]) &&
  216. mem.TypeData.Type != typeof(XmlNode[]))
  217. throw new InvalidOperationException (String.Format (errSimple2, map.TypeData.TypeName, mem.Name, mem.TypeData.TypeName));
  218. }
  219. return map;
  220. }
  221. void RegisterDerivedMap (XmlTypeMapping map, XmlTypeMapping derivedMap)
  222. {
  223. map.DerivedTypes.Add (derivedMap);
  224. map.DerivedTypes.AddRange (derivedMap.DerivedTypes);
  225. if (map.BaseMap != null)
  226. RegisterDerivedMap (map.BaseMap, derivedMap);
  227. else {
  228. XmlTypeMapping obmap = ImportTypeMapping (typeof(object));
  229. if (obmap != map)
  230. obmap.DerivedTypes.Add (derivedMap);
  231. }
  232. }
  233. string GetTypeNamespace (TypeData typeData, XmlRootAttribute root, string defaultNamespace)
  234. {
  235. string typeNamespace = null;
  236. XmlAttributes atts = null;
  237. if (!typeData.IsListType)
  238. {
  239. if (attributeOverrides != null)
  240. atts = attributeOverrides[typeData.Type];
  241. }
  242. if (atts == null)
  243. atts = new XmlAttributes (typeData.Type);
  244. if (atts.XmlType != null)
  245. {
  246. if (atts.XmlType.Namespace != null && atts.XmlType.Namespace.Length != 0 && typeData.SchemaType != SchemaTypes.Enum)
  247. typeNamespace = atts.XmlType.Namespace;
  248. }
  249. if (typeNamespace != null && typeNamespace.Length != 0) return typeNamespace;
  250. if (atts.XmlRoot != null && root == null)
  251. root = atts.XmlRoot;
  252. if (root != null)
  253. {
  254. if (root.Namespace != null && root.Namespace.Length != 0)
  255. return root.Namespace;
  256. }
  257. if (defaultNamespace == null) return "";
  258. else return defaultNamespace;
  259. }
  260. XmlTypeMapping ImportListMapping (Type type, XmlRootAttribute root, string defaultNamespace, XmlAttributes atts, int nestingLevel)
  261. {
  262. TypeData typeData = TypeTranslator.GetTypeData (type);
  263. ListMap obmap = new ListMap ();
  264. if (!allowPrivateTypes)
  265. ReflectionHelper.CheckSerializableType (type);
  266. if (atts == null) atts = new XmlAttributes();
  267. Type itemType = typeData.ListItemType;
  268. // warning: byte[][] should not be considered multiarray
  269. bool isMultiArray = (type.IsArray && (TypeTranslator.GetTypeData(itemType).SchemaType == SchemaTypes.Array) && itemType.IsArray);
  270. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  271. foreach (XmlArrayItemAttribute att in atts.XmlArrayItems)
  272. {
  273. if (att.NestingLevel != nestingLevel) continue;
  274. Type elemType = (att.Type != null) ? att.Type : itemType;
  275. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData(elemType, att.DataType));
  276. elem.Namespace = att.Namespace != null ? att.Namespace : defaultNamespace;
  277. if (elem.Namespace == null) elem.Namespace = "";
  278. elem.Form = att.Form;
  279. elem.IsNullable = att.IsNullable && CanBeNull (elem.TypeData);
  280. elem.NestingLevel = att.NestingLevel;
  281. if (isMultiArray)
  282. elem.MappedType = ImportListMapping (elemType, null, elem.Namespace, atts, nestingLevel + 1);
  283. else if (elem.TypeData.IsComplexType)
  284. elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
  285. if (att.ElementName != null) elem.ElementName = att.ElementName;
  286. else if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  287. else elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType;
  288. list.Add (elem);
  289. }
  290. if (list.Count == 0)
  291. {
  292. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData (itemType));
  293. if (isMultiArray)
  294. elem.MappedType = ImportListMapping (itemType, null, defaultNamespace, atts, nestingLevel + 1);
  295. else if (elem.TypeData.IsComplexType)
  296. elem.MappedType = ImportTypeMapping (itemType, null, defaultNamespace);
  297. if (elem.MappedType != null) elem.ElementName = elem.MappedType.XmlType;
  298. else elem.ElementName = TypeTranslator.GetTypeData(itemType).XmlType ;
  299. elem.Namespace = (defaultNamespace != null) ? defaultNamespace : "";
  300. elem.IsNullable = CanBeNull (elem.TypeData);
  301. list.Add (elem);
  302. }
  303. obmap.ItemInfo = list;
  304. // If there can be different element names (types) in the array, then its name cannot
  305. // be "ArrayOfXXX" it must be something like ArrayOfChoiceNNN
  306. string baseName;
  307. if (list.Count > 1)
  308. baseName = "ArrayOfChoice" + (arrayChoiceCount++);
  309. else
  310. {
  311. XmlTypeMapElementInfo elem = ((XmlTypeMapElementInfo)list[0]);
  312. if (elem.MappedType != null) baseName = TypeTranslator.GetArrayName (elem.MappedType.XmlType);
  313. else baseName = TypeTranslator.GetArrayName (elem.ElementName);
  314. }
  315. // Avoid name colisions
  316. int nameCount = 1;
  317. string name = baseName;
  318. do {
  319. XmlTypeMapping foundMap = helper.GetRegisteredSchemaType (name, defaultNamespace);
  320. if (foundMap == null) nameCount = -1;
  321. else if (obmap.Equals (foundMap.ObjectMap) && typeData.Type == foundMap.TypeData.Type) return foundMap;
  322. else name = baseName + (nameCount++);
  323. }
  324. while (nameCount != -1);
  325. XmlTypeMapping map = CreateTypeMapping (typeData, root, name, defaultNamespace);
  326. map.ObjectMap = obmap;
  327. // Register any of the including types as a derived class of object
  328. XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes (typeof (XmlIncludeAttribute), false);
  329. XmlTypeMapping objectMapping = ImportTypeMapping (typeof(object));
  330. for (int i = 0; i < includes.Length; i++)
  331. {
  332. Type includedType = includes[i].Type;
  333. objectMapping.DerivedTypes.Add(ImportTypeMapping (includedType, null, defaultNamespace));
  334. }
  335. // Register this map as a derived class of object
  336. helper.RegisterSchemaType (map, name, defaultNamespace);
  337. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  338. return map;
  339. }
  340. XmlTypeMapping ImportXmlNodeMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  341. {
  342. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (TypeTranslator.GetTypeData (type), root, defaultNamespace));
  343. if (map != null) return map;
  344. // Registers the maps for XmlNode and XmlElement
  345. XmlTypeMapping nodeMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlNode)), root, null, defaultNamespace);
  346. helper.RegisterClrType (nodeMap, typeof(XmlNode), nodeMap.XmlTypeNamespace);
  347. XmlTypeMapping elemMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlElement)), root, null, defaultNamespace);
  348. helper.RegisterClrType (elemMap, typeof(XmlElement), elemMap.XmlTypeNamespace);
  349. XmlTypeMapping textMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlText)), root, null, defaultNamespace);
  350. helper.RegisterClrType (elemMap, typeof(XmlText), textMap.XmlTypeNamespace);
  351. XmlTypeMapping obmap = ImportTypeMapping (typeof(object));
  352. obmap.DerivedTypes.Add (nodeMap);
  353. obmap.DerivedTypes.Add (elemMap);
  354. obmap.DerivedTypes.Add (textMap);
  355. nodeMap.DerivedTypes.Add (elemMap);
  356. nodeMap.DerivedTypes.Add (textMap);
  357. return helper.GetRegisteredClrType (type, GetTypeNamespace (TypeTranslator.GetTypeData (type), root, defaultNamespace));
  358. }
  359. XmlTypeMapping ImportPrimitiveMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  360. {
  361. TypeData typeData = TypeTranslator.GetTypeData (type);
  362. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
  363. if (map != null) return map;
  364. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  365. helper.RegisterClrType (map, type, map.XmlTypeNamespace);
  366. return map;
  367. }
  368. XmlTypeMapping ImportEnumMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  369. {
  370. TypeData typeData = TypeTranslator.GetTypeData (type);
  371. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
  372. if (map != null) return map;
  373. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  374. helper.RegisterClrType (map, type, map.XmlTypeNamespace);
  375. string [] names = Enum.GetNames (type);
  376. ArrayList members = new ArrayList();
  377. foreach (string name in names)
  378. {
  379. MemberInfo[] mem = type.GetMember (name);
  380. string xmlName = null;
  381. object[] atts = mem[0].GetCustomAttributes (typeof(XmlIgnoreAttribute), false);
  382. if (atts.Length > 0) continue;
  383. atts = mem[0].GetCustomAttributes (typeof(XmlEnumAttribute), false);
  384. if (atts.Length > 0) xmlName = ((XmlEnumAttribute)atts[0]).Name;
  385. if (xmlName == null) xmlName = name;
  386. members.Add (new EnumMap.EnumMapMember (xmlName, name));
  387. }
  388. bool isFlags = type.GetCustomAttributes (typeof(FlagsAttribute),false).Length > 0;
  389. map.ObjectMap = new EnumMap ((EnumMap.EnumMapMember[])members.ToArray (typeof(EnumMap.EnumMapMember)), isFlags);
  390. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  391. return map;
  392. }
  393. XmlTypeMapping ImportXmlSerializableMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  394. {
  395. TypeData typeData = TypeTranslator.GetTypeData (type);
  396. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
  397. if (map != null) return map;
  398. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  399. helper.RegisterClrType (map, type, map.XmlTypeNamespace);
  400. return map;
  401. }
  402. void ImportIncludedTypes (Type type, string defaultNamespace)
  403. {
  404. XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes (typeof (XmlIncludeAttribute), false);
  405. for (int n=0; n<includes.Length; n++)
  406. {
  407. Type includedType = includes[n].Type;
  408. ImportTypeMapping (includedType, null, defaultNamespace);
  409. }
  410. }
  411. ICollection GetReflectionMembers (Type type)
  412. {
  413. ArrayList members = new ArrayList();
  414. MemberInfo[] tmembers = type.GetMembers (BindingFlags.Instance | BindingFlags.Public);
  415. int currentTypePos = 0;
  416. Type currentType = null;
  417. foreach (MemberInfo tmember in tmembers)
  418. {
  419. if (currentType != tmember.DeclaringType)
  420. {
  421. currentType = tmember.DeclaringType;
  422. currentTypePos = 0;
  423. }
  424. if (tmember is FieldInfo)
  425. {
  426. FieldInfo field = tmember as FieldInfo;
  427. XmlAttributes atts = attributeOverrides[type, field.Name];
  428. if (atts == null) atts = new XmlAttributes (field);
  429. if (atts.XmlIgnore) continue;
  430. XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
  431. members.Insert (currentTypePos, member);
  432. currentTypePos++;
  433. }
  434. else if (tmember is PropertyInfo)
  435. {
  436. PropertyInfo prop = tmember as PropertyInfo;
  437. if (!prop.CanRead) continue;
  438. if (!prop.CanWrite && TypeTranslator.GetTypeData (prop.PropertyType).SchemaType != SchemaTypes.Array)
  439. continue;
  440. if (prop.GetIndexParameters().Length > 0) continue;
  441. XmlAttributes atts = attributeOverrides[type, prop.Name];
  442. if (atts == null) atts = new XmlAttributes (prop);
  443. if (atts.XmlIgnore) continue;
  444. XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
  445. members.Insert (currentTypePos, member);
  446. currentTypePos++;
  447. }
  448. }
  449. return members;
  450. }
  451. private XmlTypeMapMember CreateMapMember (XmlReflectionMember rmember, string defaultNamespace)
  452. {
  453. XmlTypeMapMember mapMember;
  454. XmlAttributes atts = rmember.XmlAttributes;
  455. TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType);
  456. if (atts.XmlAnyAttribute != null)
  457. {
  458. if ( (rmember.MemberType.FullName == "System.Xml.XmlAttribute[]") ||
  459. (rmember.MemberType.FullName == "System.Xml.XmlNode[]") )
  460. {
  461. mapMember = new XmlTypeMapMemberAnyAttribute();
  462. }
  463. else
  464. throw new InvalidOperationException ("XmlAnyAttributeAttribute can only be applied to members of type XmlAttribute[] or XmlNode[]");
  465. }
  466. else if (atts.XmlAnyElements != null && atts.XmlAnyElements.Count > 0)
  467. {
  468. if ( (rmember.MemberType.FullName == "System.Xml.XmlElement[]") ||
  469. (rmember.MemberType.FullName == "System.Xml.XmlNode[]") ||
  470. (rmember.MemberType.FullName == "System.Xml.XmlElement"))
  471. {
  472. XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement();
  473. member.ElementInfo = ImportAnyElementInfo (defaultNamespace, rmember, member, atts);
  474. mapMember = member;
  475. }
  476. else
  477. throw new InvalidOperationException ("XmlAnyElementAttribute can only be applied to members of type XmlElement, XmlElement[] or XmlNode[]");
  478. }
  479. else if (atts.Xmlns)
  480. {
  481. XmlTypeMapMemberNamespaces mapNamespaces = new XmlTypeMapMemberNamespaces ();
  482. mapMember = mapNamespaces;
  483. }
  484. else if (atts.XmlAttribute != null)
  485. {
  486. // An attribute
  487. if (atts.XmlElements != null && atts.XmlElements.Count > 0)
  488. throw new Exception ("XmlAttributeAttribute and XmlElementAttribute cannot be applied to the same member");
  489. XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute ();
  490. if (atts.XmlAttribute.AttributeName == null)
  491. mapAttribute.AttributeName = rmember.MemberName;
  492. else
  493. mapAttribute.AttributeName = atts.XmlAttribute.AttributeName;
  494. if (typeData.IsComplexType)
  495. mapAttribute.MappedType = ImportTypeMapping (typeData.Type, null, mapAttribute.Namespace);
  496. if (atts.XmlAttribute.Namespace != null && atts.XmlAttribute.Namespace != defaultNamespace)
  497. {
  498. if (atts.XmlAttribute.Form == XmlSchemaForm.Unqualified)
  499. throw new InvalidOperationException ("The Form property may not be 'Unqualified' when an explicit Namespace property is present");
  500. mapAttribute.Form = XmlSchemaForm.Qualified;
  501. mapAttribute.Namespace = atts.XmlAttribute.Namespace;
  502. }
  503. else
  504. {
  505. mapAttribute.Form = atts.XmlAttribute.Form;
  506. if (atts.XmlAttribute.Form == XmlSchemaForm.Qualified)
  507. mapAttribute.Namespace = defaultNamespace;
  508. else
  509. mapAttribute.Namespace = "";
  510. }
  511. typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.XmlAttribute.DataType);
  512. mapMember = mapAttribute;
  513. }
  514. else if (typeData.SchemaType == SchemaTypes.Array)
  515. {
  516. // If the member has a single XmlElementAttribute and the type is the type of the member,
  517. // then it is not a flat list
  518. if (atts.XmlElements.Count > 1 ||
  519. (atts.XmlElements.Count == 1 && atts.XmlElements[0].Type != typeData.Type) ||
  520. (atts.XmlText != null))
  521. {
  522. // A flat list
  523. // TODO: check that it does not have XmlArrayAttribute
  524. XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
  525. member.ListMap = new ListMap ();
  526. member.ListMap.ItemInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, typeData.ListItemType, member, atts);
  527. member.ElementInfo = member.ListMap.ItemInfo;
  528. mapMember = member;
  529. }
  530. else
  531. {
  532. // A list
  533. XmlTypeMapMemberList member = new XmlTypeMapMemberList ();
  534. // Creates an ElementInfo that identifies the array instance.
  535. member.ElementInfo = new XmlTypeMapElementInfoList();
  536. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, typeData);
  537. elem.ElementName = (atts.XmlArray != null && atts.XmlArray.ElementName != null) ? atts.XmlArray.ElementName : rmember.MemberName;
  538. elem.Namespace = (atts.XmlArray != null && atts.XmlArray.Namespace != null) ? atts.XmlArray.Namespace : defaultNamespace;
  539. elem.MappedType = ImportListMapping (rmember.MemberType, null, elem.Namespace, atts, 0);
  540. elem.IsNullable = (atts.XmlArray != null) ? atts.XmlArray.IsNullable : false;
  541. elem.Form = (atts.XmlArray != null) ? atts.XmlArray.Form : XmlSchemaForm.Qualified;
  542. member.ElementInfo.Add (elem);
  543. mapMember = member;
  544. }
  545. }
  546. else
  547. {
  548. // An element
  549. XmlTypeMapMemberElement member = new XmlTypeMapMemberElement ();
  550. member.ElementInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, rmember.MemberType, member, atts);
  551. mapMember = member;
  552. }
  553. mapMember.DefaultValue = atts.XmlDefaultValue;
  554. mapMember.TypeData = typeData;
  555. mapMember.Name = rmember.MemberName;
  556. return mapMember;
  557. }
  558. XmlTypeMapElementInfoList ImportElementInfo (string defaultName, string defaultNamespace, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  559. {
  560. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  561. ImportTextElementInfo (list, defaultType, member, atts);
  562. if (atts.XmlElements.Count == 0 && list.Count == 0)
  563. {
  564. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType));
  565. elem.ElementName = defaultName;
  566. elem.Namespace = defaultNamespace;
  567. if (elem.TypeData.IsComplexType)
  568. elem.MappedType = ImportTypeMapping (defaultType, null, defaultNamespace);
  569. list.Add (elem);
  570. }
  571. bool multiType = (atts.XmlElements.Count > 1);
  572. foreach (XmlElementAttribute att in atts.XmlElements)
  573. {
  574. Type elemType = (att.Type != null) ? att.Type : defaultType;
  575. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(elemType, att.DataType));
  576. elem.ElementName = (att.ElementName != null) ? att.ElementName : defaultName;
  577. elem.Namespace = (att.Namespace != null) ? att.Namespace : defaultNamespace;
  578. elem.Form = att.Form;
  579. elem.IsNullable = att.IsNullable;
  580. if (elem.IsNullable && elem.TypeData.IsValueType)
  581. throw new InvalidOperationException ("IsNullable may not be 'true' for value type " + elem.TypeData.FullTypeName + " in member '" + defaultName + "'");
  582. if (elem.TypeData.IsComplexType)
  583. {
  584. if (att.DataType != null) throw new InvalidOperationException ("'" + att.DataType + "' is an invalid value for the XmlElementAttribute.DateTime property. The property may only be specified for primitive types.");
  585. elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
  586. }
  587. if (att.ElementName != null)
  588. elem.ElementName = att.ElementName;
  589. else if (multiType) {
  590. if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  591. else elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType;
  592. }
  593. else
  594. elem.ElementName = defaultName;
  595. list.Add (elem);
  596. }
  597. return list;
  598. }
  599. XmlTypeMapElementInfoList ImportAnyElementInfo (string defaultNamespace, XmlReflectionMember rmember, XmlTypeMapMemberElement member, XmlAttributes atts)
  600. {
  601. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  602. ImportTextElementInfo (list, rmember.MemberType, member, atts);
  603. foreach (XmlAnyElementAttribute att in atts.XmlAnyElements)
  604. {
  605. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(typeof(XmlElement)));
  606. if (att.Name != null && att.Name != string.Empty)
  607. {
  608. elem.ElementName = att.Name;
  609. elem.Namespace = (att.Namespace != null) ? att.Namespace : "";
  610. }
  611. else
  612. {
  613. elem.IsUnnamedAnyElement = true;
  614. elem.Namespace = defaultNamespace;
  615. if (att.Namespace != null)
  616. 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.");
  617. }
  618. list.Add (elem);
  619. }
  620. return list;
  621. }
  622. void ImportTextElementInfo (XmlTypeMapElementInfoList list, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  623. {
  624. if (atts.XmlText != null)
  625. {
  626. member.IsXmlTextCollector = true;
  627. if (atts.XmlText.Type != null) defaultType = atts.XmlText.Type;
  628. if (defaultType == typeof(XmlNode)) defaultType = typeof(XmlText); // Nodes must be text nodes
  629. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType, atts.XmlText.DataType));
  630. if (elem.TypeData.SchemaType != SchemaTypes.Primitive &&
  631. elem.TypeData.SchemaType != SchemaTypes.Enum &&
  632. elem.TypeData.SchemaType != SchemaTypes.XmlNode &&
  633. !(elem.TypeData.SchemaType == SchemaTypes.Array && elem.TypeData.ListItemTypeData.SchemaType == SchemaTypes.XmlNode)
  634. )
  635. throw new InvalidOperationException ("XmlText cannot be used to encode complex types");
  636. elem.IsTextElement = true;
  637. elem.WrappedElement = false;
  638. list.Add (elem);
  639. }
  640. }
  641. bool CanBeNull (TypeData type)
  642. {
  643. return (type.SchemaType != SchemaTypes.Primitive || type.Type == typeof (string));
  644. }
  645. public void IncludeType (Type type)
  646. {
  647. if (type == null)
  648. throw new ArgumentNullException ("type");
  649. if (includedTypes == null) includedTypes = new ArrayList ();
  650. if (!includedTypes.Contains (type))
  651. includedTypes.Add (type);
  652. }
  653. public void IncludeTypes (ICustomAttributeProvider provider)
  654. {
  655. object[] ats = provider.GetCustomAttributes (typeof(XmlIncludeAttribute), true);
  656. foreach (XmlIncludeAttribute at in ats)
  657. IncludeType (at.Type);
  658. }
  659. #endregion // Methods
  660. }
  661. }