XmlReflectionImporter.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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 (textMap, typeof(XmlText), textMap.XmlTypeNamespace);
  351. XmlTypeMapping docMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlDocument)), root, null, defaultNamespace);
  352. helper.RegisterClrType (docMap, typeof(XmlDocument), textMap.XmlTypeNamespace);
  353. XmlTypeMapping obmap = ImportTypeMapping (typeof(object));
  354. obmap.DerivedTypes.Add (nodeMap);
  355. obmap.DerivedTypes.Add (elemMap);
  356. obmap.DerivedTypes.Add (textMap);
  357. obmap.DerivedTypes.Add (docMap);
  358. nodeMap.DerivedTypes.Add (elemMap);
  359. nodeMap.DerivedTypes.Add (textMap);
  360. nodeMap.DerivedTypes.Add (docMap);
  361. map = helper.GetRegisteredClrType (type, GetTypeNamespace (TypeTranslator.GetTypeData (type), root, defaultNamespace));
  362. if (map == null) throw new InvalidOperationException ("Objects of type '" + type + "' can't be serialized");
  363. return map;
  364. }
  365. XmlTypeMapping ImportPrimitiveMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  366. {
  367. TypeData typeData = TypeTranslator.GetTypeData (type);
  368. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
  369. if (map != null) return map;
  370. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  371. helper.RegisterClrType (map, type, map.XmlTypeNamespace);
  372. return map;
  373. }
  374. XmlTypeMapping ImportEnumMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  375. {
  376. TypeData typeData = TypeTranslator.GetTypeData (type);
  377. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
  378. if (map != null) return map;
  379. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  380. helper.RegisterClrType (map, type, map.XmlTypeNamespace);
  381. string [] names = Enum.GetNames (type);
  382. ArrayList members = new ArrayList();
  383. foreach (string name in names)
  384. {
  385. MemberInfo[] mem = type.GetMember (name);
  386. string xmlName = null;
  387. object[] atts = mem[0].GetCustomAttributes (typeof(XmlIgnoreAttribute), false);
  388. if (atts.Length > 0) continue;
  389. atts = mem[0].GetCustomAttributes (typeof(XmlEnumAttribute), false);
  390. if (atts.Length > 0) xmlName = ((XmlEnumAttribute)atts[0]).Name;
  391. if (xmlName == null) xmlName = name;
  392. members.Add (new EnumMap.EnumMapMember (xmlName, name));
  393. }
  394. bool isFlags = type.GetCustomAttributes (typeof(FlagsAttribute),false).Length > 0;
  395. map.ObjectMap = new EnumMap ((EnumMap.EnumMapMember[])members.ToArray (typeof(EnumMap.EnumMapMember)), isFlags);
  396. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  397. return map;
  398. }
  399. XmlTypeMapping ImportXmlSerializableMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  400. {
  401. TypeData typeData = TypeTranslator.GetTypeData (type);
  402. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
  403. if (map != null) return map;
  404. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  405. helper.RegisterClrType (map, type, map.XmlTypeNamespace);
  406. return map;
  407. }
  408. void ImportIncludedTypes (Type type, string defaultNamespace)
  409. {
  410. XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes (typeof (XmlIncludeAttribute), false);
  411. for (int n=0; n<includes.Length; n++)
  412. {
  413. Type includedType = includes[n].Type;
  414. ImportTypeMapping (includedType, null, defaultNamespace);
  415. }
  416. }
  417. ICollection GetReflectionMembers (Type type)
  418. {
  419. ArrayList members = new ArrayList();
  420. MemberInfo[] tmembers = type.GetMembers (BindingFlags.Instance | BindingFlags.Public);
  421. int currentTypePos = 0;
  422. Type currentType = null;
  423. foreach (MemberInfo tmember in tmembers)
  424. {
  425. if (currentType != tmember.DeclaringType)
  426. {
  427. currentType = tmember.DeclaringType;
  428. currentTypePos = 0;
  429. }
  430. if (tmember is FieldInfo)
  431. {
  432. FieldInfo field = tmember as FieldInfo;
  433. XmlAttributes atts = attributeOverrides[type, field.Name];
  434. if (atts == null) atts = new XmlAttributes (field);
  435. if (atts.XmlIgnore) continue;
  436. XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
  437. members.Insert (currentTypePos, member);
  438. currentTypePos++;
  439. }
  440. else if (tmember is PropertyInfo)
  441. {
  442. PropertyInfo prop = tmember as PropertyInfo;
  443. if (!prop.CanRead) continue;
  444. if (!prop.CanWrite && TypeTranslator.GetTypeData (prop.PropertyType).SchemaType != SchemaTypes.Array)
  445. continue;
  446. if (prop.GetIndexParameters().Length > 0) continue;
  447. XmlAttributes atts = attributeOverrides[type, prop.Name];
  448. if (atts == null) atts = new XmlAttributes (prop);
  449. if (atts.XmlIgnore) continue;
  450. XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
  451. members.Insert (currentTypePos, member);
  452. currentTypePos++;
  453. }
  454. }
  455. return members;
  456. }
  457. private XmlTypeMapMember CreateMapMember (XmlReflectionMember rmember, string defaultNamespace)
  458. {
  459. XmlTypeMapMember mapMember;
  460. XmlAttributes atts = rmember.XmlAttributes;
  461. TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType);
  462. if (atts.XmlAnyAttribute != null)
  463. {
  464. if ( (rmember.MemberType.FullName == "System.Xml.XmlAttribute[]") ||
  465. (rmember.MemberType.FullName == "System.Xml.XmlNode[]") )
  466. {
  467. mapMember = new XmlTypeMapMemberAnyAttribute();
  468. }
  469. else
  470. throw new InvalidOperationException ("XmlAnyAttributeAttribute can only be applied to members of type XmlAttribute[] or XmlNode[]");
  471. }
  472. else if (atts.XmlAnyElements != null && atts.XmlAnyElements.Count > 0)
  473. {
  474. if ( (rmember.MemberType.FullName == "System.Xml.XmlElement[]") ||
  475. (rmember.MemberType.FullName == "System.Xml.XmlNode[]") ||
  476. (rmember.MemberType.FullName == "System.Xml.XmlElement"))
  477. {
  478. XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement();
  479. member.ElementInfo = ImportAnyElementInfo (defaultNamespace, rmember, member, atts);
  480. mapMember = member;
  481. }
  482. else
  483. throw new InvalidOperationException ("XmlAnyElementAttribute can only be applied to members of type XmlElement, XmlElement[] or XmlNode[]");
  484. }
  485. else if (atts.Xmlns)
  486. {
  487. XmlTypeMapMemberNamespaces mapNamespaces = new XmlTypeMapMemberNamespaces ();
  488. mapMember = mapNamespaces;
  489. }
  490. else if (atts.XmlAttribute != null)
  491. {
  492. // An attribute
  493. if (atts.XmlElements != null && atts.XmlElements.Count > 0)
  494. throw new Exception ("XmlAttributeAttribute and XmlElementAttribute cannot be applied to the same member");
  495. XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute ();
  496. if (atts.XmlAttribute.AttributeName == null)
  497. mapAttribute.AttributeName = rmember.MemberName;
  498. else
  499. mapAttribute.AttributeName = atts.XmlAttribute.AttributeName;
  500. if (typeData.IsComplexType)
  501. mapAttribute.MappedType = ImportTypeMapping (typeData.Type, null, mapAttribute.Namespace);
  502. if (atts.XmlAttribute.Namespace != null && atts.XmlAttribute.Namespace != defaultNamespace)
  503. {
  504. if (atts.XmlAttribute.Form == XmlSchemaForm.Unqualified)
  505. throw new InvalidOperationException ("The Form property may not be 'Unqualified' when an explicit Namespace property is present");
  506. mapAttribute.Form = XmlSchemaForm.Qualified;
  507. mapAttribute.Namespace = atts.XmlAttribute.Namespace;
  508. }
  509. else
  510. {
  511. mapAttribute.Form = atts.XmlAttribute.Form;
  512. if (atts.XmlAttribute.Form == XmlSchemaForm.Qualified)
  513. mapAttribute.Namespace = defaultNamespace;
  514. else
  515. mapAttribute.Namespace = "";
  516. }
  517. typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.XmlAttribute.DataType);
  518. mapMember = mapAttribute;
  519. }
  520. else if (typeData.SchemaType == SchemaTypes.Array)
  521. {
  522. // If the member has a single XmlElementAttribute and the type is the type of the member,
  523. // then it is not a flat list
  524. if (atts.XmlElements.Count > 1 ||
  525. (atts.XmlElements.Count == 1 && atts.XmlElements[0].Type != typeData.Type) ||
  526. (atts.XmlText != null))
  527. {
  528. // A flat list
  529. // TODO: check that it does not have XmlArrayAttribute
  530. XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
  531. member.ListMap = new ListMap ();
  532. member.ListMap.ItemInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, typeData.ListItemType, member, atts);
  533. member.ElementInfo = member.ListMap.ItemInfo;
  534. mapMember = member;
  535. }
  536. else
  537. {
  538. // A list
  539. XmlTypeMapMemberList member = new XmlTypeMapMemberList ();
  540. // Creates an ElementInfo that identifies the array instance.
  541. member.ElementInfo = new XmlTypeMapElementInfoList();
  542. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, typeData);
  543. elem.ElementName = (atts.XmlArray != null && atts.XmlArray.ElementName != null) ? atts.XmlArray.ElementName : rmember.MemberName;
  544. elem.Namespace = (atts.XmlArray != null && atts.XmlArray.Namespace != null) ? atts.XmlArray.Namespace : defaultNamespace;
  545. elem.MappedType = ImportListMapping (rmember.MemberType, null, elem.Namespace, atts, 0);
  546. elem.IsNullable = (atts.XmlArray != null) ? atts.XmlArray.IsNullable : false;
  547. elem.Form = (atts.XmlArray != null) ? atts.XmlArray.Form : XmlSchemaForm.Qualified;
  548. member.ElementInfo.Add (elem);
  549. mapMember = member;
  550. }
  551. }
  552. else
  553. {
  554. // An element
  555. XmlTypeMapMemberElement member = new XmlTypeMapMemberElement ();
  556. member.ElementInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, rmember.MemberType, member, atts);
  557. mapMember = member;
  558. }
  559. mapMember.DefaultValue = atts.XmlDefaultValue;
  560. mapMember.TypeData = typeData;
  561. mapMember.Name = rmember.MemberName;
  562. return mapMember;
  563. }
  564. XmlTypeMapElementInfoList ImportElementInfo (string defaultName, string defaultNamespace, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  565. {
  566. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  567. ImportTextElementInfo (list, defaultType, member, atts);
  568. if (atts.XmlElements.Count == 0 && list.Count == 0)
  569. {
  570. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType));
  571. elem.ElementName = defaultName;
  572. elem.Namespace = defaultNamespace;
  573. if (elem.TypeData.IsComplexType)
  574. elem.MappedType = ImportTypeMapping (defaultType, null, defaultNamespace);
  575. list.Add (elem);
  576. }
  577. bool multiType = (atts.XmlElements.Count > 1);
  578. foreach (XmlElementAttribute att in atts.XmlElements)
  579. {
  580. Type elemType = (att.Type != null) ? att.Type : defaultType;
  581. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(elemType, att.DataType));
  582. elem.ElementName = (att.ElementName != null) ? att.ElementName : defaultName;
  583. elem.Namespace = (att.Namespace != null) ? att.Namespace : defaultNamespace;
  584. elem.Form = att.Form;
  585. elem.IsNullable = att.IsNullable;
  586. if (elem.IsNullable && elem.TypeData.IsValueType)
  587. throw new InvalidOperationException ("IsNullable may not be 'true' for value type " + elem.TypeData.FullTypeName + " in member '" + defaultName + "'");
  588. if (elem.TypeData.IsComplexType)
  589. {
  590. 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.");
  591. elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
  592. }
  593. if (att.ElementName != null)
  594. elem.ElementName = att.ElementName;
  595. else if (multiType) {
  596. if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  597. else elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType;
  598. }
  599. else
  600. elem.ElementName = defaultName;
  601. list.Add (elem);
  602. }
  603. return list;
  604. }
  605. XmlTypeMapElementInfoList ImportAnyElementInfo (string defaultNamespace, XmlReflectionMember rmember, XmlTypeMapMemberElement member, XmlAttributes atts)
  606. {
  607. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  608. ImportTextElementInfo (list, rmember.MemberType, member, atts);
  609. foreach (XmlAnyElementAttribute att in atts.XmlAnyElements)
  610. {
  611. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(typeof(XmlElement)));
  612. if (att.Name != null && att.Name != string.Empty)
  613. {
  614. elem.ElementName = att.Name;
  615. elem.Namespace = (att.Namespace != null) ? att.Namespace : "";
  616. }
  617. else
  618. {
  619. elem.IsUnnamedAnyElement = true;
  620. elem.Namespace = defaultNamespace;
  621. if (att.Namespace != null)
  622. 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.");
  623. }
  624. list.Add (elem);
  625. }
  626. return list;
  627. }
  628. void ImportTextElementInfo (XmlTypeMapElementInfoList list, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  629. {
  630. if (atts.XmlText != null)
  631. {
  632. member.IsXmlTextCollector = true;
  633. if (atts.XmlText.Type != null) defaultType = atts.XmlText.Type;
  634. if (defaultType == typeof(XmlNode)) defaultType = typeof(XmlText); // Nodes must be text nodes
  635. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType, atts.XmlText.DataType));
  636. if (elem.TypeData.SchemaType != SchemaTypes.Primitive &&
  637. elem.TypeData.SchemaType != SchemaTypes.Enum &&
  638. elem.TypeData.SchemaType != SchemaTypes.XmlNode &&
  639. !(elem.TypeData.SchemaType == SchemaTypes.Array && elem.TypeData.ListItemTypeData.SchemaType == SchemaTypes.XmlNode)
  640. )
  641. throw new InvalidOperationException ("XmlText cannot be used to encode complex types");
  642. elem.IsTextElement = true;
  643. elem.WrappedElement = false;
  644. list.Add (elem);
  645. }
  646. }
  647. bool CanBeNull (TypeData type)
  648. {
  649. return (type.SchemaType != SchemaTypes.Primitive || type.Type == typeof (string));
  650. }
  651. public void IncludeType (Type type)
  652. {
  653. if (type == null)
  654. throw new ArgumentNullException ("type");
  655. if (includedTypes == null) includedTypes = new ArrayList ();
  656. if (!includedTypes.Contains (type))
  657. includedTypes.Add (type);
  658. }
  659. public void IncludeTypes (ICustomAttributeProvider provider)
  660. {
  661. object[] ats = provider.GetCustomAttributes (typeof(XmlIncludeAttribute), true);
  662. foreach (XmlIncludeAttribute at in ats)
  663. IncludeType (at.Type);
  664. }
  665. #endregion // Methods
  666. }
  667. }