XmlReflectionImporter.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. }
  228. string GetTypeNamespace (TypeData typeData, XmlRootAttribute root, string defaultNamespace)
  229. {
  230. string typeNamespace = null;
  231. XmlAttributes atts = null;
  232. if (!typeData.IsListType)
  233. {
  234. if (attributeOverrides != null)
  235. atts = attributeOverrides[typeData.Type];
  236. }
  237. if (atts == null)
  238. atts = new XmlAttributes (typeData.Type);
  239. if (atts.XmlType != null)
  240. {
  241. if (atts.XmlType.Namespace != null && atts.XmlType.Namespace.Length != 0 && typeData.SchemaType != SchemaTypes.Enum)
  242. typeNamespace = atts.XmlType.Namespace;
  243. }
  244. if (typeNamespace != null && typeNamespace.Length != 0) return typeNamespace;
  245. if (atts.XmlRoot != null && root == null)
  246. root = atts.XmlRoot;
  247. if (root != null)
  248. {
  249. if (root.Namespace != null && root.Namespace.Length != 0)
  250. return root.Namespace;
  251. }
  252. if (defaultNamespace == null) return "";
  253. else return defaultNamespace;
  254. }
  255. XmlTypeMapping ImportListMapping (Type type, XmlRootAttribute root, string defaultNamespace, XmlAttributes atts, int nestingLevel)
  256. {
  257. TypeData typeData = TypeTranslator.GetTypeData (type);
  258. ListMap obmap = new ListMap ();
  259. if (!allowPrivateTypes)
  260. ReflectionHelper.CheckSerializableType (type);
  261. if (atts == null) atts = new XmlAttributes();
  262. Type itemType = typeData.ListItemType;
  263. // warning: byte[][] should not be considered multiarray
  264. bool isMultiArray = (type.IsArray && (TypeTranslator.GetTypeData(itemType).SchemaType == SchemaTypes.Array) && itemType.IsArray);
  265. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  266. foreach (XmlArrayItemAttribute att in atts.XmlArrayItems)
  267. {
  268. if (att.NestingLevel != nestingLevel) continue;
  269. Type elemType = (att.Type != null) ? att.Type : itemType;
  270. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData(elemType, att.DataType));
  271. elem.Namespace = att.Namespace != null ? att.Namespace : defaultNamespace;
  272. if (elem.Namespace == null) elem.Namespace = "";
  273. elem.Form = att.Form;
  274. elem.IsNullable = att.IsNullable && CanBeNull (elem.TypeData);
  275. elem.NestingLevel = att.NestingLevel;
  276. if (isMultiArray)
  277. elem.MappedType = ImportListMapping (elemType, null, elem.Namespace, atts, nestingLevel + 1);
  278. else if (elem.TypeData.IsComplexType)
  279. elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
  280. if (att.ElementName != null) elem.ElementName = att.ElementName;
  281. else if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  282. else elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType;
  283. list.Add (elem);
  284. }
  285. if (list.Count == 0)
  286. {
  287. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData (itemType));
  288. if (isMultiArray)
  289. elem.MappedType = ImportListMapping (itemType, null, defaultNamespace, atts, nestingLevel + 1);
  290. else if (elem.TypeData.IsComplexType)
  291. elem.MappedType = ImportTypeMapping (itemType, null, defaultNamespace);
  292. if (elem.MappedType != null) elem.ElementName = elem.MappedType.XmlType;
  293. else elem.ElementName = TypeTranslator.GetTypeData(itemType).XmlType ;
  294. elem.Namespace = (defaultNamespace != null) ? defaultNamespace : "";
  295. elem.IsNullable = CanBeNull (elem.TypeData);
  296. list.Add (elem);
  297. }
  298. obmap.ItemInfo = list;
  299. // If there can be different element names (types) in the array, then its name cannot
  300. // be "ArrayOfXXX" it must be something like ArrayOfChoiceNNN
  301. string baseName;
  302. if (list.Count > 1)
  303. baseName = "ArrayOfChoice" + (arrayChoiceCount++);
  304. else
  305. {
  306. XmlTypeMapElementInfo elem = ((XmlTypeMapElementInfo)list[0]);
  307. if (elem.MappedType != null) baseName = TypeTranslator.GetArrayName (elem.MappedType.XmlType);
  308. else baseName = TypeTranslator.GetArrayName (elem.ElementName);
  309. }
  310. // Avoid name colisions
  311. int nameCount = 1;
  312. string name = baseName;
  313. do {
  314. XmlTypeMapping foundMap = helper.GetRegisteredSchemaType (name, defaultNamespace);
  315. if (foundMap == null) nameCount = -1;
  316. else if (obmap.Equals (foundMap.ObjectMap) && typeData.Type == foundMap.TypeData.Type) return foundMap;
  317. else name = baseName + (nameCount++);
  318. }
  319. while (nameCount != -1);
  320. XmlTypeMapping map = CreateTypeMapping (typeData, root, name, defaultNamespace);
  321. map.ObjectMap = obmap;
  322. // Register any of the including types as a derived class of object
  323. XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes (typeof (XmlIncludeAttribute), false);
  324. XmlTypeMapping objectMapping = ImportTypeMapping (typeof(object));
  325. for (int i = 0; i < includes.Length; i++)
  326. {
  327. Type includedType = includes[i].Type;
  328. objectMapping.DerivedTypes.Add(ImportTypeMapping (includedType, null, defaultNamespace));
  329. }
  330. // Register this map as a derived class of object
  331. helper.RegisterSchemaType (map, name, defaultNamespace);
  332. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  333. return map;
  334. }
  335. XmlTypeMapping ImportXmlNodeMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  336. {
  337. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (TypeTranslator.GetTypeData (type), root, defaultNamespace));
  338. if (map != null) return map;
  339. // Registers the maps for XmlNode and XmlElement
  340. XmlTypeMapping nodeMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlNode)), root, null, defaultNamespace);
  341. helper.RegisterClrType (nodeMap, typeof(XmlNode), nodeMap.XmlTypeNamespace);
  342. XmlTypeMapping elemMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlElement)), root, null, defaultNamespace);
  343. helper.RegisterClrType (elemMap, typeof(XmlElement), elemMap.XmlTypeNamespace);
  344. XmlTypeMapping textMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlText)), root, null, defaultNamespace);
  345. helper.RegisterClrType (elemMap, typeof(XmlText), textMap.XmlTypeNamespace);
  346. XmlTypeMapping obmap = ImportTypeMapping (typeof(object));
  347. obmap.DerivedTypes.Add (nodeMap);
  348. obmap.DerivedTypes.Add (elemMap);
  349. obmap.DerivedTypes.Add (textMap);
  350. nodeMap.DerivedTypes.Add (elemMap);
  351. nodeMap.DerivedTypes.Add (textMap);
  352. return helper.GetRegisteredClrType (type, GetTypeNamespace (TypeTranslator.GetTypeData (type), root, defaultNamespace));
  353. }
  354. XmlTypeMapping ImportPrimitiveMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  355. {
  356. TypeData typeData = TypeTranslator.GetTypeData (type);
  357. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
  358. if (map != null) return map;
  359. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  360. helper.RegisterClrType (map, type, map.XmlTypeNamespace);
  361. return map;
  362. }
  363. XmlTypeMapping ImportEnumMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  364. {
  365. TypeData typeData = TypeTranslator.GetTypeData (type);
  366. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
  367. if (map != null) return map;
  368. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  369. helper.RegisterClrType (map, type, map.XmlTypeNamespace);
  370. string [] names = Enum.GetNames (type);
  371. ArrayList members = new ArrayList();
  372. foreach (string name in names)
  373. {
  374. MemberInfo[] mem = type.GetMember (name);
  375. string xmlName = null;
  376. object[] atts = mem[0].GetCustomAttributes (typeof(XmlIgnoreAttribute), false);
  377. if (atts.Length > 0) continue;
  378. atts = mem[0].GetCustomAttributes (typeof(XmlEnumAttribute), false);
  379. if (atts.Length > 0) xmlName = ((XmlEnumAttribute)atts[0]).Name;
  380. if (xmlName == null) xmlName = name;
  381. members.Add (new EnumMap.EnumMapMember (xmlName, name));
  382. }
  383. bool isFlags = type.GetCustomAttributes (typeof(FlagsAttribute),false).Length > 0;
  384. map.ObjectMap = new EnumMap ((EnumMap.EnumMapMember[])members.ToArray (typeof(EnumMap.EnumMapMember)), isFlags);
  385. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  386. return map;
  387. }
  388. XmlTypeMapping ImportXmlSerializableMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  389. {
  390. TypeData typeData = TypeTranslator.GetTypeData (type);
  391. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
  392. if (map != null) return map;
  393. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  394. helper.RegisterClrType (map, type, map.XmlTypeNamespace);
  395. return map;
  396. }
  397. void ImportIncludedTypes (Type type, string defaultNamespace)
  398. {
  399. XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes (typeof (XmlIncludeAttribute), false);
  400. for (int n=0; n<includes.Length; n++)
  401. {
  402. Type includedType = includes[n].Type;
  403. ImportTypeMapping (includedType, null, defaultNamespace);
  404. }
  405. }
  406. ICollection GetReflectionMembers (Type type)
  407. {
  408. ArrayList members = new ArrayList();
  409. MemberInfo[] tmembers = type.GetMembers (BindingFlags.Instance | BindingFlags.Public);
  410. int currentTypePos = 0;
  411. Type currentType = null;
  412. foreach (MemberInfo tmember in tmembers)
  413. {
  414. if (currentType != tmember.DeclaringType)
  415. {
  416. currentType = tmember.DeclaringType;
  417. currentTypePos = 0;
  418. }
  419. if (tmember is FieldInfo)
  420. {
  421. FieldInfo field = tmember as FieldInfo;
  422. XmlAttributes atts = attributeOverrides[type, field.Name];
  423. if (atts == null) atts = new XmlAttributes (field);
  424. if (atts.XmlIgnore) continue;
  425. XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
  426. members.Insert (currentTypePos, member);
  427. currentTypePos++;
  428. }
  429. else if (tmember is PropertyInfo)
  430. {
  431. PropertyInfo prop = tmember as PropertyInfo;
  432. if (!prop.CanRead) continue;
  433. if (!prop.CanWrite && TypeTranslator.GetTypeData (prop.PropertyType).SchemaType != SchemaTypes.Array)
  434. continue;
  435. if (prop.GetIndexParameters().Length > 0) continue;
  436. XmlAttributes atts = attributeOverrides[type, prop.Name];
  437. if (atts == null) atts = new XmlAttributes (prop);
  438. if (atts.XmlIgnore) continue;
  439. XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
  440. members.Insert (currentTypePos, member);
  441. currentTypePos++;
  442. }
  443. }
  444. return members;
  445. }
  446. private XmlTypeMapMember CreateMapMember (XmlReflectionMember rmember, string defaultNamespace)
  447. {
  448. XmlTypeMapMember mapMember;
  449. XmlAttributes atts = rmember.XmlAttributes;
  450. TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType);
  451. if (atts.XmlAnyAttribute != null)
  452. {
  453. if ( (rmember.MemberType.FullName == "System.Xml.XmlAttribute[]") ||
  454. (rmember.MemberType.FullName == "System.Xml.XmlNode[]") )
  455. {
  456. mapMember = new XmlTypeMapMemberAnyAttribute();
  457. }
  458. else
  459. throw new InvalidOperationException ("XmlAnyAttributeAttribute can only be applied to members of type XmlAttribute[] or XmlNode[]");
  460. }
  461. else if (atts.XmlAnyElements != null && atts.XmlAnyElements.Count > 0)
  462. {
  463. if ( (rmember.MemberType.FullName == "System.Xml.XmlElement[]") ||
  464. (rmember.MemberType.FullName == "System.Xml.XmlNode[]") ||
  465. (rmember.MemberType.FullName == "System.Xml.XmlElement"))
  466. {
  467. XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement();
  468. member.ElementInfo = ImportAnyElementInfo (defaultNamespace, rmember, member, atts);
  469. mapMember = member;
  470. }
  471. else
  472. throw new InvalidOperationException ("XmlAnyElementAttribute can only be applied to members of type XmlElement, XmlElement[] or XmlNode[]");
  473. }
  474. else if (atts.Xmlns)
  475. {
  476. XmlTypeMapMemberNamespaces mapNamespaces = new XmlTypeMapMemberNamespaces ();
  477. mapMember = mapNamespaces;
  478. }
  479. else if (atts.XmlAttribute != null)
  480. {
  481. // An attribute
  482. if (atts.XmlElements != null && atts.XmlElements.Count > 0)
  483. throw new Exception ("XmlAttributeAttribute and XmlElementAttribute cannot be applied to the same member");
  484. XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute ();
  485. if (atts.XmlAttribute.AttributeName == null)
  486. mapAttribute.AttributeName = rmember.MemberName;
  487. else
  488. mapAttribute.AttributeName = atts.XmlAttribute.AttributeName;
  489. if (typeData.IsComplexType)
  490. mapAttribute.MappedType = ImportTypeMapping (typeData.Type, null, mapAttribute.Namespace);
  491. if (atts.XmlAttribute.Namespace != null && atts.XmlAttribute.Namespace != defaultNamespace)
  492. {
  493. if (atts.XmlAttribute.Form == XmlSchemaForm.Unqualified)
  494. throw new InvalidOperationException ("The Form property may not be 'Unqualified' when an explicit Namespace property is present");
  495. mapAttribute.Form = XmlSchemaForm.Qualified;
  496. mapAttribute.Namespace = atts.XmlAttribute.Namespace;
  497. }
  498. else
  499. {
  500. mapAttribute.Form = atts.XmlAttribute.Form;
  501. if (atts.XmlAttribute.Form == XmlSchemaForm.Qualified)
  502. mapAttribute.Namespace = defaultNamespace;
  503. else
  504. mapAttribute.Namespace = "";
  505. }
  506. typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.XmlAttribute.DataType);
  507. mapMember = mapAttribute;
  508. }
  509. else if (typeData.SchemaType == SchemaTypes.Array)
  510. {
  511. // If the member has a single XmlElementAttribute and the type is the type of the member,
  512. // then it is not a flat list
  513. if (atts.XmlElements.Count > 1 ||
  514. (atts.XmlElements.Count == 1 && atts.XmlElements[0].Type != typeData.Type) ||
  515. (atts.XmlText != null))
  516. {
  517. // A flat list
  518. // TODO: check that it does not have XmlArrayAttribute
  519. XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
  520. member.ListMap = new ListMap ();
  521. member.ListMap.ItemInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, typeData.ListItemType, member, atts);
  522. member.ElementInfo = member.ListMap.ItemInfo;
  523. mapMember = member;
  524. }
  525. else
  526. {
  527. // A list
  528. XmlTypeMapMemberList member = new XmlTypeMapMemberList ();
  529. // Creates an ElementInfo that identifies the array instance.
  530. member.ElementInfo = new XmlTypeMapElementInfoList();
  531. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, typeData);
  532. elem.ElementName = (atts.XmlArray != null && atts.XmlArray.ElementName != null) ? atts.XmlArray.ElementName : rmember.MemberName;
  533. elem.Namespace = (atts.XmlArray != null && atts.XmlArray.Namespace != null) ? atts.XmlArray.Namespace : defaultNamespace;
  534. elem.MappedType = ImportListMapping (rmember.MemberType, null, elem.Namespace, atts, 0);
  535. elem.IsNullable = (atts.XmlArray != null) ? atts.XmlArray.IsNullable : false;
  536. elem.Form = (atts.XmlArray != null) ? atts.XmlArray.Form : XmlSchemaForm.Qualified;
  537. member.ElementInfo.Add (elem);
  538. mapMember = member;
  539. }
  540. }
  541. else
  542. {
  543. // An element
  544. XmlTypeMapMemberElement member = new XmlTypeMapMemberElement ();
  545. member.ElementInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, rmember.MemberType, member, atts);
  546. mapMember = member;
  547. }
  548. mapMember.DefaultValue = atts.XmlDefaultValue;
  549. mapMember.TypeData = typeData;
  550. mapMember.Name = rmember.MemberName;
  551. return mapMember;
  552. }
  553. XmlTypeMapElementInfoList ImportElementInfo (string defaultName, string defaultNamespace, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  554. {
  555. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  556. ImportTextElementInfo (list, defaultType, member, atts);
  557. if (atts.XmlElements.Count == 0 && list.Count == 0)
  558. {
  559. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType));
  560. elem.ElementName = defaultName;
  561. elem.Namespace = defaultNamespace;
  562. if (elem.TypeData.IsComplexType)
  563. elem.MappedType = ImportTypeMapping (defaultType, null, defaultNamespace);
  564. list.Add (elem);
  565. }
  566. bool multiType = (atts.XmlElements.Count > 1);
  567. foreach (XmlElementAttribute att in atts.XmlElements)
  568. {
  569. Type elemType = (att.Type != null) ? att.Type : defaultType;
  570. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(elemType, att.DataType));
  571. elem.ElementName = (att.ElementName != null) ? att.ElementName : defaultName;
  572. elem.Namespace = (att.Namespace != null) ? att.Namespace : defaultNamespace;
  573. elem.Form = att.Form;
  574. elem.IsNullable = att.IsNullable;
  575. if (elem.IsNullable && elem.TypeData.IsValueType)
  576. throw new InvalidOperationException ("IsNullable may not be 'true' for value type " + elem.TypeData.FullTypeName + " in member '" + defaultName + "'");
  577. if (elem.TypeData.IsComplexType)
  578. {
  579. 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.");
  580. elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
  581. }
  582. if (att.ElementName != null)
  583. elem.ElementName = att.ElementName;
  584. else if (multiType) {
  585. if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  586. else elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType;
  587. }
  588. else
  589. elem.ElementName = defaultName;
  590. list.Add (elem);
  591. }
  592. return list;
  593. }
  594. XmlTypeMapElementInfoList ImportAnyElementInfo (string defaultNamespace, XmlReflectionMember rmember, XmlTypeMapMemberElement member, XmlAttributes atts)
  595. {
  596. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  597. ImportTextElementInfo (list, rmember.MemberType, member, atts);
  598. foreach (XmlAnyElementAttribute att in atts.XmlAnyElements)
  599. {
  600. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(typeof(XmlElement)));
  601. if (att.Name != null && att.Name != string.Empty)
  602. {
  603. elem.ElementName = att.Name;
  604. elem.Namespace = (att.Namespace != null) ? att.Namespace : "";
  605. }
  606. else
  607. {
  608. elem.IsUnnamedAnyElement = true;
  609. elem.Namespace = defaultNamespace;
  610. if (att.Namespace != null)
  611. 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.");
  612. }
  613. list.Add (elem);
  614. }
  615. return list;
  616. }
  617. void ImportTextElementInfo (XmlTypeMapElementInfoList list, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  618. {
  619. if (atts.XmlText != null)
  620. {
  621. member.IsXmlTextCollector = true;
  622. if (atts.XmlText.Type != null) defaultType = atts.XmlText.Type;
  623. if (defaultType == typeof(XmlNode)) defaultType = typeof(XmlText); // Nodes must be text nodes
  624. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType, atts.XmlText.DataType));
  625. if (elem.TypeData.SchemaType != SchemaTypes.Primitive &&
  626. elem.TypeData.SchemaType != SchemaTypes.Enum &&
  627. elem.TypeData.SchemaType != SchemaTypes.XmlNode &&
  628. !(elem.TypeData.SchemaType == SchemaTypes.Array && elem.TypeData.ListItemTypeData.SchemaType == SchemaTypes.XmlNode)
  629. )
  630. throw new InvalidOperationException ("XmlText cannot be used to encode complex types");
  631. elem.IsTextElement = true;
  632. elem.WrappedElement = false;
  633. list.Add (elem);
  634. }
  635. }
  636. bool CanBeNull (TypeData type)
  637. {
  638. return (type.SchemaType != SchemaTypes.Primitive || type.Type == typeof (string));
  639. }
  640. public void IncludeType (Type type)
  641. {
  642. if (type == null)
  643. throw new ArgumentNullException ("type");
  644. if (includedTypes == null) includedTypes = new ArrayList ();
  645. if (!includedTypes.Contains (type))
  646. includedTypes.Add (type);
  647. }
  648. public void IncludeTypes (ICustomAttributeProvider provider)
  649. {
  650. object[] ats = provider.GetCustomAttributes (typeof(XmlIncludeAttribute), true);
  651. foreach (XmlIncludeAttribute at in ats)
  652. IncludeType (at.Type);
  653. }
  654. #endregion // Methods
  655. }
  656. }