XmlReflectionImporter.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. namespace System.Xml.Serialization {
  15. public class XmlReflectionImporter {
  16. string initialDefaultNamespace;
  17. XmlAttributeOverrides attributeOverrides;
  18. ArrayList includedTypes;
  19. Hashtable clrTypes = new Hashtable ();
  20. Hashtable schemaTypes = new Hashtable ();
  21. int arrayChoiceCount = 1;
  22. #region Constructors
  23. public XmlReflectionImporter ()
  24. : this (null, null)
  25. {
  26. }
  27. public XmlReflectionImporter (string defaultNamespace)
  28. : this (null, defaultNamespace)
  29. {
  30. }
  31. public XmlReflectionImporter (XmlAttributeOverrides attributeOverrides)
  32. : this (attributeOverrides, null)
  33. {
  34. }
  35. public XmlReflectionImporter (XmlAttributeOverrides attributeOverrides, string defaultNamespace)
  36. {
  37. if (defaultNamespace == null)
  38. this.initialDefaultNamespace = String.Empty;
  39. else
  40. this.initialDefaultNamespace = defaultNamespace;
  41. if (attributeOverrides == null)
  42. this.attributeOverrides = new XmlAttributeOverrides();
  43. else
  44. this.attributeOverrides = attributeOverrides;
  45. }
  46. #endregion // Constructors
  47. #region Methods
  48. public XmlMembersMapping ImportMembersMapping (string elementName,
  49. string ns,
  50. XmlReflectionMember [] members,
  51. bool hasWrapperElement)
  52. {
  53. XmlMemberMapping[] mapping = new XmlMemberMapping[members.Length];
  54. for (int n=0; n<members.Length; n++)
  55. {
  56. XmlTypeMapMember mapMem = CreateMapMember (members[n], ns);
  57. mapping[n] = new XmlMemberMapping (members[n], mapMem);
  58. }
  59. return new XmlMembersMapping (elementName, ns, hasWrapperElement, mapping);
  60. }
  61. public XmlTypeMapping ImportTypeMapping (Type type)
  62. {
  63. return ImportTypeMapping (type, null, null);
  64. }
  65. public XmlTypeMapping ImportTypeMapping (Type type, string defaultNamespace)
  66. {
  67. return ImportTypeMapping (type, null, defaultNamespace);
  68. }
  69. public XmlTypeMapping ImportTypeMapping (Type type, XmlRootAttribute group)
  70. {
  71. return ImportTypeMapping (type, group, null);
  72. }
  73. public XmlTypeMapping ImportTypeMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  74. {
  75. if (type == null)
  76. throw new ArgumentNullException ("type");
  77. if (type == typeof (void))
  78. throw new InvalidOperationException ("Type " + type.Name + " may not be serialized.");
  79. if (defaultNamespace == null) defaultNamespace = initialDefaultNamespace;
  80. if (defaultNamespace == null) defaultNamespace = string.Empty;
  81. switch (TypeTranslator.GetTypeData(type).SchemaType)
  82. {
  83. case SchemaTypes.Class: return ImportClassMapping (type, root, defaultNamespace);
  84. case SchemaTypes.Array: return ImportListMapping (type, root, defaultNamespace, null, 0);
  85. case SchemaTypes.XmlNode: return ImportXmlNodeMapping (type, root, defaultNamespace);
  86. case SchemaTypes.Primitive: return ImportPrimitiveMapping (type, root, defaultNamespace);
  87. case SchemaTypes.Enum: return ImportEnumMapping (type, root, defaultNamespace);
  88. case SchemaTypes.DataSet:
  89. default: throw new NotSupportedException ("Type " + type.FullName + " not supported for XML stialization");
  90. }
  91. }
  92. XmlTypeMapping CreateTypeMapping (TypeData typeData, XmlRootAttribute root, string defaultXmlType, string defaultNamespace)
  93. {
  94. string membersNamespace = defaultNamespace;
  95. string elementName;
  96. XmlAttributes atts = null;
  97. if (defaultXmlType == null) defaultXmlType = typeData.XmlType;
  98. if (!typeData.IsListType)
  99. {
  100. if (attributeOverrides != null)
  101. atts = attributeOverrides[typeData.Type];
  102. }
  103. if (atts == null)
  104. atts = new XmlAttributes (typeData.Type);
  105. if (atts.XmlRoot != null && root == null)
  106. root = atts.XmlRoot;
  107. if (atts.XmlType != null)
  108. {
  109. if (atts.XmlType.Namespace != null && atts.XmlType.Namespace != string.Empty)
  110. membersNamespace = atts.XmlType.Namespace;
  111. if (atts.XmlType.TypeName != null && atts.XmlType.TypeName != string.Empty)
  112. defaultXmlType = atts.XmlType.TypeName;
  113. }
  114. elementName = defaultXmlType;
  115. if (root != null)
  116. {
  117. if (root.ElementName != null && root.ElementName != String.Empty)
  118. elementName = root.ElementName;
  119. if (root.Namespace != null && root.Namespace != String.Empty)
  120. membersNamespace = root.Namespace;
  121. }
  122. if (membersNamespace == null) membersNamespace = "";
  123. XmlTypeMapping map = new XmlTypeMapping (elementName, membersNamespace, typeData, defaultXmlType);
  124. return map;
  125. }
  126. XmlTypeMapping ImportClassMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  127. {
  128. TypeData typeData = TypeTranslator.GetTypeData (type);
  129. XmlTypeMapping map = GetRegisteredClrType (type, defaultNamespace);
  130. if (map != null) return map;
  131. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  132. RegisterClrType (map, type, defaultNamespace);
  133. RegisterSchemaType (map, map.XmlType, defaultNamespace);
  134. ClassMap classMap = new ClassMap ();
  135. map.ObjectMap = classMap;
  136. // Import members
  137. try
  138. {
  139. ICollection members = GetReflectionMembers (type);
  140. foreach (XmlReflectionMember rmember in members)
  141. {
  142. if (rmember.XmlAttributes.XmlIgnore) continue;
  143. classMap.AddMember (CreateMapMember (rmember, map.Namespace));
  144. }
  145. }
  146. catch (Exception ex) {
  147. throw CreateError (map, ex.Message);
  148. }
  149. // Import derived classes
  150. XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes (typeof (XmlIncludeAttribute), false);
  151. for (int n=0; n<includes.Length; n++)
  152. {
  153. Type includedType = includes[n].Type;
  154. if (!includedType.IsSubclassOf(type)) throw CreateError (map, "Type '" + includedType.FullName + "' is not a subclass of '" + type.FullName + "'");
  155. map.DerivedTypes.Add (ImportTypeMapping (includedType, root, defaultNamespace));
  156. }
  157. if (type == typeof (object) && includedTypes != null)
  158. {
  159. foreach (Type intype in includedTypes)
  160. map.DerivedTypes.Add (ImportTypeMapping (intype, defaultNamespace));
  161. }
  162. // Register this map as a derived class of object
  163. if (typeData.Type != typeof(object))
  164. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  165. return map;
  166. }
  167. XmlTypeMapping ImportListMapping (Type type, XmlRootAttribute root, string defaultNamespace, XmlAttributes atts, int nestingLevel)
  168. {
  169. TypeData typeData = TypeTranslator.GetTypeData (type);
  170. ListMap obmap = new ListMap ();
  171. if (atts == null) atts = new XmlAttributes();
  172. Type itemType = typeData.ListItemType;
  173. // warning: byte[][] should not be considered multiarray
  174. bool isMultiArray = (type.IsArray && (TypeTranslator.GetTypeData(itemType).SchemaType == SchemaTypes.Array) && itemType.IsArray);
  175. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  176. foreach (XmlArrayItemAttribute att in atts.XmlArrayItems)
  177. {
  178. if (att.NestingLevel != nestingLevel) continue;
  179. Type elemType = (att.Type != null) ? att.Type : itemType;
  180. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData(elemType));
  181. elem.DataType = att.DataType;
  182. elem.Namespace = att.Namespace != null ? att.Namespace : "";
  183. elem.Form = att.Form;
  184. elem.IsNullable = att.IsNullable;
  185. elem.NestingLevel = att.NestingLevel;
  186. if (isMultiArray)
  187. elem.MappedType = ImportListMapping (elemType, null, elem.Namespace, atts, nestingLevel + 1);
  188. else if (elem.TypeData.IsComplexType)
  189. elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
  190. if (att.ElementName != null) elem.ElementName = att.ElementName;
  191. else if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  192. else elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType;
  193. list.Add (elem);
  194. }
  195. if (list.Count == 0)
  196. {
  197. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData (itemType));
  198. if (isMultiArray)
  199. elem.MappedType = ImportListMapping (itemType, null, defaultNamespace, atts, nestingLevel + 1);
  200. else if (elem.TypeData.IsComplexType)
  201. elem.MappedType = ImportTypeMapping (itemType, null, defaultNamespace);
  202. if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  203. else elem.ElementName = TypeTranslator.GetTypeData(itemType).XmlType ;
  204. elem.Namespace = (defaultNamespace != null) ? defaultNamespace : "";
  205. elem.IsNullable = true; // By default, items are nullable
  206. list.Add (elem);
  207. }
  208. obmap.ItemInfo = list;
  209. // If there can be different element names (types) in the array, then its name cannot
  210. // be "ArrayOfXXX" it must be something like ArrayOfChoiceNNN
  211. string baseName;
  212. if (list.Count > 1)
  213. baseName = "ArrayOfChoice" + (arrayChoiceCount++);
  214. else
  215. {
  216. XmlTypeMapElementInfo elem = ((XmlTypeMapElementInfo)list[0]);
  217. if (elem.MappedType != null) baseName = GetArrayName (elem.MappedType.ElementName);
  218. else baseName = GetArrayName (elem.ElementName);
  219. }
  220. // Avoid name colisions
  221. int nameCount = 1;
  222. string name = baseName;
  223. do {
  224. XmlTypeMapping foundMap = GetRegisteredSchemaType (name, defaultNamespace);
  225. if (foundMap == null) nameCount = -1;
  226. else if (obmap.Equals (foundMap.ObjectMap)) return foundMap;
  227. else name = baseName + (nameCount++);
  228. }
  229. while (nameCount != -1);
  230. XmlTypeMapping map = CreateTypeMapping (typeData, root, name, defaultNamespace);
  231. map.ObjectMap = obmap;
  232. // Register this map as a derived class of object
  233. RegisterSchemaType (map, name, defaultNamespace);
  234. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  235. return map;
  236. }
  237. string GetArrayName (string elemName)
  238. {
  239. return "ArrayOf" + Char.ToUpper (elemName [0]) + elemName.Substring (1);
  240. }
  241. XmlTypeMapping ImportXmlNodeMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  242. {
  243. XmlTypeMapping map = GetRegisteredClrType (type, defaultNamespace);
  244. if (map != null) return map;
  245. // Registers the maps for XmlNode and XmlElement
  246. XmlTypeMapping nodeMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlNode)), root, null, defaultNamespace);
  247. RegisterClrType (nodeMap, typeof(XmlNode), defaultNamespace);
  248. XmlTypeMapping elemMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlElement)), root, null, defaultNamespace);
  249. RegisterClrType (elemMap, typeof(XmlElement), defaultNamespace);
  250. ImportTypeMapping (typeof(object)).DerivedTypes.Add (nodeMap);
  251. ImportTypeMapping (typeof(object)).DerivedTypes.Add (elemMap);
  252. nodeMap.DerivedTypes.Add (elemMap);
  253. return GetRegisteredClrType (type, defaultNamespace);
  254. }
  255. XmlTypeMapping ImportPrimitiveMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  256. {
  257. XmlTypeMapping map = GetRegisteredClrType (type, defaultNamespace);
  258. if (map != null) return map;
  259. map = CreateTypeMapping (TypeTranslator.GetTypeData (type), root, null, defaultNamespace);
  260. RegisterClrType (map, type, defaultNamespace);
  261. return map;
  262. }
  263. XmlTypeMapping ImportEnumMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  264. {
  265. XmlTypeMapping map = GetRegisteredClrType (type, defaultNamespace);
  266. if (map != null) return map;
  267. map = CreateTypeMapping (TypeTranslator.GetTypeData (type), root, null, defaultNamespace);
  268. RegisterClrType (map, type, defaultNamespace);
  269. string [] names = Enum.GetNames (type);
  270. EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember[names.Length];
  271. for (int n=0; n<names.Length; n++)
  272. {
  273. MemberInfo[] mem = type.GetMember (names[n]);
  274. string xmlName = names[n];
  275. object[] atts = mem[0].GetCustomAttributes (typeof(XmlEnumAttribute), false);
  276. if (atts.Length > 0) xmlName = ((XmlEnumAttribute)atts[0]).Name;
  277. members[n] = new EnumMap.EnumMapMember (xmlName, names[n]);
  278. }
  279. map.ObjectMap = new EnumMap (members);
  280. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  281. return map;
  282. }
  283. public ICollection GetReflectionMembers (Type type)
  284. {
  285. ArrayList members = new ArrayList();
  286. PropertyInfo[] properties = type.GetProperties (BindingFlags.Instance | BindingFlags.Public);
  287. foreach (PropertyInfo prop in properties)
  288. {
  289. if (!prop.CanRead) continue;
  290. XmlAttributes atts = attributeOverrides[type, prop.Name];
  291. if (atts == null) atts = new XmlAttributes (prop);
  292. if (atts.XmlIgnore) continue;
  293. XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
  294. members.Add (member);
  295. }
  296. FieldInfo[] fields = type.GetFields (BindingFlags.Instance | BindingFlags.Public);
  297. foreach (FieldInfo field in fields)
  298. {
  299. XmlAttributes atts = attributeOverrides[type, field.Name];
  300. if (atts == null) atts = new XmlAttributes (field);
  301. if (atts.XmlIgnore) continue;
  302. XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
  303. members.Add (member);
  304. }
  305. return members;
  306. }
  307. private XmlTypeMapMember CreateMapMember (XmlReflectionMember rmember, string defaultNamespace)
  308. {
  309. XmlTypeMapMember mapMember;
  310. XmlAttributes atts = rmember.XmlAttributes;
  311. TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType);
  312. if (atts.XmlAnyAttribute != null)
  313. {
  314. if ( (rmember.MemberType.FullName == "System.Xml.XmlAttribute[]") ||
  315. (rmember.MemberType.FullName == "System.Xml.XmlNode[]") )
  316. {
  317. mapMember = new XmlTypeMapMemberAnyAttribute();
  318. }
  319. else
  320. throw new InvalidOperationException ("XmlAnyAttributeAttribute can only be applied to members of type XmlAttribute[] or XmlNode[]");
  321. }
  322. else if (atts.XmlAnyElements != null && atts.XmlAnyElements.Count > 0)
  323. {
  324. if ( (rmember.MemberType.FullName == "System.Xml.XmlElement[]") ||
  325. (rmember.MemberType.FullName == "System.Xml.XmlNode[]") ||
  326. (rmember.MemberType.FullName == "System.Xml.XmlElement"))
  327. {
  328. XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement();
  329. member.ElementInfo = ImportAnyElementInfo (defaultNamespace, member, atts);
  330. mapMember = member;
  331. }
  332. else
  333. throw new InvalidOperationException ("XmlAnyElementAttribute can only be applied to members of type XmlElement, XmlElement[] or XmlNode[]");
  334. }
  335. else if (atts.XmlAttribute != null)
  336. {
  337. // An attribute
  338. if (atts.XmlElements != null && atts.XmlElements.Count > 0)
  339. throw new Exception ("XmlAttributeAttribute and XmlElementAttribute cannot be applied to the same member");
  340. XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute ();
  341. if (atts.XmlAttribute.AttributeName == null)
  342. mapAttribute.AttributeName = rmember.MemberName;
  343. else
  344. mapAttribute.AttributeName = atts.XmlAttribute.AttributeName;
  345. mapAttribute.DataType = atts.XmlAttribute.DataType;
  346. mapAttribute.Form = atts.XmlAttribute.Form;
  347. mapAttribute.Namespace = (atts.XmlAttribute != null) ? atts.XmlAttribute.Namespace : "";
  348. mapMember = mapAttribute;
  349. }
  350. else if (typeData.SchemaType == SchemaTypes.Array)
  351. {
  352. if (atts.XmlElements.Count > 0)
  353. {
  354. // A flat list
  355. // TODO: check that it does not have XmlArrayAttribute
  356. XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
  357. member.ListMap = new ListMap ();
  358. member.ListMap.ItemInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, typeData.ListItemType, member, atts);
  359. member.ElementInfo = member.ListMap.ItemInfo;
  360. mapMember = member;
  361. }
  362. else
  363. {
  364. // A list
  365. XmlTypeMapMemberList member = new XmlTypeMapMemberList ();
  366. member.ElementName = (atts.XmlArray != null && atts.XmlArray.ElementName != null) ? atts.XmlArray.ElementName : rmember.MemberName;
  367. member.Namespace = (atts.XmlArray != null && atts.XmlArray.Namespace != null) ? atts.XmlArray.Namespace : defaultNamespace;
  368. member.ListTypeMapping = ImportListMapping (rmember.MemberType, null, member.Namespace, atts, 0);
  369. // Creates an ElementInfo that identifies the array instance.
  370. member.ElementInfo = new XmlTypeMapElementInfoList();
  371. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, typeData);
  372. elem.ElementName = member.ElementName;
  373. elem.Namespace = member.Namespace;
  374. elem.MappedType = member.ListTypeMapping;
  375. member.ElementInfo.Add (elem);
  376. mapMember = member;
  377. }
  378. }
  379. else
  380. {
  381. // An element
  382. XmlTypeMapMemberElement member = new XmlTypeMapMemberElement ();
  383. member.ElementInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, rmember.MemberType, member, atts);
  384. mapMember = member;
  385. }
  386. mapMember.TypeData = typeData;
  387. mapMember.Name = rmember.MemberName;
  388. return mapMember;
  389. }
  390. XmlTypeMapElementInfoList ImportElementInfo (string defaultName, string defaultNamespace, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  391. {
  392. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  393. if (atts.XmlElements.Count == 0)
  394. {
  395. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType));
  396. elem.ElementName = defaultName;
  397. elem.Namespace = defaultNamespace;
  398. if (elem.TypeData.IsComplexType)
  399. elem.MappedType = ImportTypeMapping (defaultType, null, defaultNamespace);
  400. list.Add (elem);
  401. }
  402. bool multiType = (atts.XmlElements.Count > 1);
  403. foreach (XmlElementAttribute att in atts.XmlElements)
  404. {
  405. Type elemType = (att.Type != null) ? att.Type : defaultType;
  406. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(elemType));
  407. elem.ElementName = (att.ElementName != null) ? att.ElementName : defaultName;
  408. elem.DataType = att.DataType;
  409. elem.Namespace = (att.Namespace != null) ? att.Namespace : defaultNamespace;
  410. elem.Form = att.Form;
  411. elem.IsNullable = att.IsNullable;
  412. if (elem.TypeData.IsComplexType)
  413. elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
  414. if (att.ElementName != null)
  415. elem.ElementName = att.ElementName;
  416. else if (multiType) {
  417. if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  418. else elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType;
  419. }
  420. else
  421. elem.ElementName = defaultName;
  422. list.Add (elem);
  423. }
  424. return list;
  425. }
  426. XmlTypeMapElementInfoList ImportAnyElementInfo (string defaultNamespace, XmlTypeMapMemberElement member, XmlAttributes atts)
  427. {
  428. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  429. foreach (XmlAnyElementAttribute att in atts.XmlAnyElements)
  430. {
  431. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(typeof(XmlElement)));
  432. elem.ElementName = (att.Name != null) ? att.Name : "";
  433. elem.Namespace = (att.Namespace != null) ? att.Namespace : "";
  434. list.Add (elem);
  435. }
  436. return list;
  437. }
  438. public void IncludeType (Type type)
  439. {
  440. if (type == null)
  441. throw new ArgumentNullException ("type");
  442. if (includedTypes == null) includedTypes = new ArrayList ();
  443. includedTypes.Add (type);
  444. }
  445. void RegisterSchemaType (XmlTypeMapping map, string xmlType, string ns)
  446. {
  447. string mapKey = xmlType + "/" + ns;
  448. if (!schemaTypes.ContainsKey (xmlType))
  449. schemaTypes.Add (mapKey, map);
  450. }
  451. XmlTypeMapping GetRegisteredSchemaType (string xmlType, string ns)
  452. {
  453. string mapKey = xmlType + "/" + ns;
  454. return schemaTypes[mapKey] as XmlTypeMapping;
  455. }
  456. void RegisterClrType (XmlTypeMapping map, Type type, string ns)
  457. {
  458. if (type == typeof(object)) ns = "";
  459. string mapKey = type.FullName + "/" + ns;
  460. if (!clrTypes.ContainsKey (mapKey))
  461. clrTypes.Add (mapKey, map);
  462. }
  463. XmlTypeMapping GetRegisteredClrType (Type type, string ns)
  464. {
  465. if (type == typeof(object)) ns = "";
  466. string mapKey = type.FullName + "/" + ns;
  467. return clrTypes[mapKey] as XmlTypeMapping;
  468. }
  469. Exception CreateError (XmlTypeMapping map, string message)
  470. {
  471. return new InvalidOperationException ("There was an error reflecting '" + map.TypeFullName + "': " + message);
  472. }
  473. #endregion // Methods
  474. }
  475. }