SoapReflectionImporter.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. //
  2. // System.Xml.Serialization.SoapReflectionImporter
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2002
  8. //
  9. using System.Reflection;
  10. using System.Xml;
  11. using System.Xml.Schema;
  12. using System.Collections;
  13. namespace System.Xml.Serialization {
  14. public class SoapReflectionImporter {
  15. SoapAttributeOverrides attributeOverrides;
  16. string initialDefaultNamespace;
  17. ArrayList includedTypes;
  18. ArrayList relatedMaps = new ArrayList ();
  19. ReflectionHelper helper = new ReflectionHelper();
  20. #region Constructors
  21. public SoapReflectionImporter (): this (null, null)
  22. {
  23. }
  24. public SoapReflectionImporter (SoapAttributeOverrides attributeOverrides): this (attributeOverrides, null)
  25. {
  26. }
  27. public SoapReflectionImporter (string defaultNamespace): this (null, defaultNamespace)
  28. {
  29. }
  30. public SoapReflectionImporter (SoapAttributeOverrides attributeOverrides, string defaultNamespace)
  31. {
  32. if (defaultNamespace == null) initialDefaultNamespace = String.Empty;
  33. else initialDefaultNamespace = defaultNamespace;
  34. if (attributeOverrides == null) this.attributeOverrides = new SoapAttributeOverrides();
  35. else this.attributeOverrides = attributeOverrides;
  36. }
  37. #endregion // Constructors
  38. #region Methods
  39. public XmlMembersMapping ImportMembersMapping (string elementName, string ns, XmlReflectionMember[] members)
  40. {
  41. return ImportMembersMapping (elementName, ns, members, true, true, false);
  42. }
  43. public XmlMembersMapping ImportMembersMapping (string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors)
  44. {
  45. return ImportMembersMapping (elementName, ns, members, hasWrapperElement, writeAccessors, false);
  46. }
  47. public XmlMembersMapping ImportMembersMapping (string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors, bool validate)
  48. {
  49. XmlMemberMapping[] mapping = new XmlMemberMapping[members.Length];
  50. for (int n=0; n<members.Length; n++)
  51. {
  52. XmlTypeMapMember mapMem = CreateMapMember (members[n], ns);
  53. mapping[n] = new XmlMemberMapping (members[n].MemberName, ns, mapMem, true);
  54. }
  55. XmlMembersMapping mps = new XmlMembersMapping (elementName, ns, hasWrapperElement, writeAccessors, mapping);
  56. mps.RelatedMaps = relatedMaps;
  57. mps.Format = SerializationFormat.Encoded;
  58. mps.Source = new MembersSerializationSource (elementName, hasWrapperElement, members, writeAccessors, false, null, includedTypes);
  59. return mps;
  60. }
  61. public XmlTypeMapping ImportTypeMapping (Type type)
  62. {
  63. return ImportTypeMapping (type, null);
  64. }
  65. public XmlTypeMapping ImportTypeMapping (Type type, string defaultNamespace)
  66. {
  67. if (type == null)
  68. throw new ArgumentNullException ("type");
  69. if (type == typeof (void))
  70. throw new InvalidOperationException ("Type " + type.Name + " may not be serialized.");
  71. if (defaultNamespace == null) defaultNamespace = initialDefaultNamespace;
  72. if (defaultNamespace == null) defaultNamespace = string.Empty;
  73. XmlTypeMapping map;
  74. switch (TypeTranslator.GetTypeData(type).SchemaType)
  75. {
  76. case SchemaTypes.Class: map = ImportClassMapping (type, defaultNamespace); break;
  77. case SchemaTypes.Array: map = ImportListMapping (type, defaultNamespace); break;
  78. case SchemaTypes.XmlNode: throw CreateTypeException (type);
  79. case SchemaTypes.Primitive: map = ImportPrimitiveMapping (type, defaultNamespace); break;
  80. case SchemaTypes.Enum: map = ImportEnumMapping (type, defaultNamespace); break;
  81. case SchemaTypes.XmlSerializable:
  82. default: throw new NotSupportedException ("Type " + type.FullName + " not supported for XML stialization");
  83. }
  84. map.RelatedMaps = relatedMaps;
  85. map.Format = SerializationFormat.Encoded;
  86. map.Source = new SoapTypeSerializationSource (type, attributeOverrides, defaultNamespace, includedTypes);
  87. return map;
  88. }
  89. XmlTypeMapping CreateTypeMapping (TypeData typeData, string defaultXmlType, string defaultNamespace)
  90. {
  91. string membersNamespace = defaultNamespace;
  92. bool includeInSchema = true;
  93. SoapAttributes atts = null;
  94. if (defaultXmlType == null) defaultXmlType = typeData.XmlType;
  95. if (!typeData.IsListType)
  96. {
  97. if (attributeOverrides != null)
  98. atts = attributeOverrides[typeData.Type];
  99. if (atts != null && typeData.SchemaType == SchemaTypes.Primitive)
  100. throw new InvalidOperationException ("SoapType attribute may not be specified for the type " + typeData.FullTypeName);
  101. }
  102. if (atts == null)
  103. atts = new SoapAttributes (typeData.Type);
  104. if (atts.SoapType != null)
  105. {
  106. if (atts.SoapType.Namespace != null && atts.SoapType.Namespace != string.Empty)
  107. membersNamespace = atts.SoapType.Namespace;
  108. if (atts.SoapType.TypeName != null && atts.SoapType.TypeName != string.Empty)
  109. defaultXmlType = atts.SoapType.TypeName;
  110. includeInSchema = atts.SoapType.IncludeInSchema;
  111. }
  112. if (membersNamespace == null) membersNamespace = "";
  113. XmlTypeMapping map = new XmlTypeMapping (defaultXmlType, membersNamespace, typeData, defaultXmlType, membersNamespace);
  114. map.IncludeInSchema = includeInSchema;
  115. relatedMaps.Add (map);
  116. return map;
  117. }
  118. XmlTypeMapping ImportClassMapping (Type type, string defaultNamespace)
  119. {
  120. if (type.IsValueType) throw CreateStructException (type);
  121. if (type == typeof (object)) defaultNamespace = XmlSchema.Namespace;
  122. ReflectionHelper.CheckSerializableType (type);
  123. TypeData typeData = TypeTranslator.GetTypeData (type);
  124. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, defaultNamespace));
  125. if (map != null) return map;
  126. map = CreateTypeMapping (typeData, null, defaultNamespace);
  127. helper.RegisterClrType (map, type, map.Namespace);
  128. map.MultiReferenceType = true;
  129. ClassMap classMap = new ClassMap ();
  130. map.ObjectMap = classMap;
  131. // Import members
  132. try
  133. {
  134. ICollection members = GetReflectionMembers (type);
  135. foreach (XmlReflectionMember rmember in members)
  136. {
  137. if (rmember.SoapAttributes.SoapIgnore) continue;
  138. classMap.AddMember (CreateMapMember (rmember, map.Namespace));
  139. }
  140. }
  141. catch (Exception ex)
  142. {
  143. throw helper.CreateError (map, ex.Message);
  144. }
  145. // Import included classes
  146. SoapIncludeAttribute[] includes = (SoapIncludeAttribute[])type.GetCustomAttributes (typeof (SoapIncludeAttribute), false);
  147. for (int n=0; n<includes.Length; n++)
  148. {
  149. Type includedType = includes[n].Type;
  150. ImportTypeMapping (includedType);
  151. }
  152. if (type == typeof (object) && includedTypes != null)
  153. {
  154. foreach (Type intype in includedTypes)
  155. map.DerivedTypes.Add (ImportTypeMapping (intype));
  156. }
  157. // Register inheritance relations
  158. if (type.BaseType != null)
  159. {
  160. XmlTypeMapping bmap = ImportClassMapping (type.BaseType, defaultNamespace);
  161. if (type.BaseType != typeof (object))
  162. map.BaseMap = bmap;
  163. // At this point, derived classes of this map must be already registered
  164. RegisterDerivedMap (bmap, map);
  165. }
  166. return map;
  167. }
  168. void RegisterDerivedMap (XmlTypeMapping map, XmlTypeMapping derivedMap)
  169. {
  170. map.DerivedTypes.Add (derivedMap);
  171. map.DerivedTypes.AddRange (derivedMap.DerivedTypes);
  172. if (map.BaseMap != null)
  173. RegisterDerivedMap (map.BaseMap, derivedMap);
  174. else {
  175. XmlTypeMapping obmap = ImportTypeMapping (typeof(object));
  176. if (obmap != map)
  177. obmap.DerivedTypes.Add (derivedMap);
  178. }
  179. }
  180. string GetTypeNamespace (TypeData typeData, string defaultNamespace)
  181. {
  182. string membersNamespace = defaultNamespace;
  183. SoapAttributes atts = null;
  184. if (!typeData.IsListType)
  185. {
  186. if (attributeOverrides != null)
  187. atts = attributeOverrides[typeData.Type];
  188. }
  189. if (atts == null)
  190. atts = new SoapAttributes (typeData.Type);
  191. if (atts.SoapType != null)
  192. {
  193. if (atts.SoapType.Namespace != null && atts.SoapType.Namespace != string.Empty)
  194. membersNamespace = atts.SoapType.Namespace;
  195. }
  196. if (membersNamespace == null) return "";
  197. else return membersNamespace;
  198. }
  199. XmlTypeMapping ImportListMapping (Type type, string defaultNamespace)
  200. {
  201. TypeData typeData = TypeTranslator.GetTypeData (type);
  202. XmlTypeMapping map = helper.GetRegisteredClrType (type, XmlSerializer.EncodingNamespace);
  203. if (map != null) return map;
  204. ListMap obmap = new ListMap ();
  205. TypeData itemTypeData = typeData.ListItemTypeData;
  206. map = CreateTypeMapping (typeData, "Array", XmlSerializer.EncodingNamespace);
  207. helper.RegisterClrType (map, type, XmlSerializer.EncodingNamespace);
  208. map.MultiReferenceType = true;
  209. map.ObjectMap = obmap;
  210. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, itemTypeData);
  211. if (elem.TypeData.IsComplexType) {
  212. elem.MappedType = ImportTypeMapping (typeData.ListItemType, defaultNamespace);
  213. elem.TypeData = elem.MappedType.TypeData;
  214. }
  215. elem.ElementName = "Item";
  216. elem.Namespace = string.Empty;
  217. elem.IsNullable = true; // By default, items are nullable
  218. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  219. list.Add (elem);
  220. obmap.ItemInfo = list;
  221. XmlTypeMapping objMap = ImportTypeMapping (typeof(object), defaultNamespace);
  222. objMap.DerivedTypes.Add (map);
  223. // Register any of the including types as a derived class of object
  224. SoapIncludeAttribute[] includes = (SoapIncludeAttribute[])type.GetCustomAttributes (typeof (SoapIncludeAttribute), false);
  225. for (int i = 0; i < includes.Length; i++)
  226. {
  227. Type includedType = includes[i].Type;
  228. objMap.DerivedTypes.Add(ImportTypeMapping (includedType, defaultNamespace));
  229. }
  230. return map;
  231. }
  232. XmlTypeMapping ImportPrimitiveMapping (Type type, string defaultNamespace)
  233. {
  234. TypeData typeData = TypeTranslator.GetTypeData (type);
  235. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, defaultNamespace));
  236. if (map != null) return map;
  237. map = CreateTypeMapping (typeData, null, defaultNamespace);
  238. helper.RegisterClrType (map, type, map.Namespace);
  239. return map;
  240. }
  241. XmlTypeMapping ImportEnumMapping (Type type, string defaultNamespace)
  242. {
  243. TypeData typeData = TypeTranslator.GetTypeData (type);
  244. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, defaultNamespace));
  245. if (map != null) return map;
  246. map = CreateTypeMapping (typeData, null, defaultNamespace);
  247. helper.RegisterClrType (map, type, map.Namespace);
  248. map.MultiReferenceType = true;
  249. string [] names = Enum.GetNames (type);
  250. EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember[names.Length];
  251. for (int n=0; n<names.Length; n++)
  252. {
  253. MemberInfo[] mem = type.GetMember (names[n]);
  254. string xmlName = names[n];
  255. object[] atts = mem[0].GetCustomAttributes (typeof(SoapEnumAttribute), false);
  256. if (atts.Length > 0) xmlName = ((SoapEnumAttribute)atts[0]).Name;
  257. members[n] = new EnumMap.EnumMapMember (xmlName, names[n]);
  258. }
  259. bool isFlags = type.GetCustomAttributes (typeof(FlagsAttribute),false).Length > 0;
  260. map.ObjectMap = new EnumMap (members, isFlags);
  261. ImportTypeMapping (typeof(object), defaultNamespace).DerivedTypes.Add (map);
  262. return map;
  263. }
  264. ICollection GetReflectionMembers (Type type)
  265. {
  266. ArrayList members = new ArrayList();
  267. PropertyInfo[] properties = type.GetProperties (BindingFlags.Instance | BindingFlags.Public);
  268. foreach (PropertyInfo prop in properties)
  269. {
  270. if (!prop.CanRead) continue;
  271. if (!prop.CanWrite && TypeTranslator.GetTypeData (prop.PropertyType).SchemaType != SchemaTypes.Array)
  272. continue;
  273. SoapAttributes atts = attributeOverrides[type, prop.Name];
  274. if (atts == null) atts = new SoapAttributes (prop);
  275. if (atts.SoapIgnore) continue;
  276. XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
  277. members.Add (member);
  278. }
  279. FieldInfo[] fields = type.GetFields (BindingFlags.Instance | BindingFlags.Public);
  280. foreach (FieldInfo field in fields)
  281. {
  282. SoapAttributes atts = attributeOverrides[type, field.Name];
  283. if (atts == null) atts = new SoapAttributes (field);
  284. if (atts.SoapIgnore) continue;
  285. XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
  286. members.Add (member);
  287. }
  288. return members;
  289. }
  290. private XmlTypeMapMember CreateMapMember (XmlReflectionMember rmember, string defaultNamespace)
  291. {
  292. XmlTypeMapMember mapMember;
  293. SoapAttributes atts = rmember.SoapAttributes;
  294. TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType);
  295. if (atts.SoapAttribute != null)
  296. {
  297. // An attribute
  298. if (atts.SoapElement != null)
  299. throw new Exception ("SoapAttributeAttribute and SoapElementAttribute cannot be applied to the same member");
  300. XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute ();
  301. if (atts.SoapAttribute.AttributeName == null)
  302. mapAttribute.AttributeName = rmember.MemberName;
  303. else
  304. mapAttribute.AttributeName = atts.SoapAttribute.AttributeName;
  305. mapAttribute.Namespace = (atts.SoapAttribute.Namespace != null) ? atts.SoapAttribute.Namespace : "";
  306. if (typeData.IsComplexType)
  307. mapAttribute.MappedType = ImportTypeMapping (typeData.Type, defaultNamespace);
  308. typeData = TypeTranslator.GetTypeData (rmember.MemberType, atts.SoapAttribute.DataType);
  309. mapMember = mapAttribute;
  310. }
  311. else
  312. {
  313. if (typeData.SchemaType == SchemaTypes.Array) mapMember = new XmlTypeMapMemberList ();
  314. else mapMember = new XmlTypeMapMemberElement ();
  315. if (atts.SoapElement != null && atts.SoapElement.DataType != null)
  316. typeData = TypeTranslator.GetTypeData (rmember.MemberType, atts.SoapElement.DataType);
  317. // Creates an ElementInfo that identifies the element
  318. XmlTypeMapElementInfoList infoList = new XmlTypeMapElementInfoList();
  319. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (mapMember, typeData);
  320. elem.ElementName = (atts.SoapElement != null && atts.SoapElement.ElementName != null) ? atts.SoapElement.ElementName : rmember.MemberName;
  321. elem.Namespace = string.Empty;
  322. elem.IsNullable = (atts.SoapElement != null) ? atts.SoapElement.IsNullable : false;
  323. if (typeData.IsComplexType)
  324. elem.MappedType = ImportTypeMapping (typeData.Type, defaultNamespace);
  325. infoList.Add (elem);
  326. ((XmlTypeMapMemberElement)mapMember).ElementInfo = infoList;
  327. }
  328. mapMember.TypeData = typeData;
  329. mapMember.Name = rmember.MemberName;
  330. return mapMember;
  331. }
  332. public void IncludeType (Type type)
  333. {
  334. if (type == null)
  335. throw new ArgumentNullException ("type");
  336. if (includedTypes == null) includedTypes = new ArrayList ();
  337. if (!includedTypes.Contains (type))
  338. includedTypes.Add (type);
  339. }
  340. public void IncludeTypes (ICustomAttributeProvider provider)
  341. {
  342. object[] ats = provider.GetCustomAttributes (typeof(SoapIncludeAttribute), true);
  343. foreach (SoapIncludeAttribute at in ats)
  344. IncludeType (at.Type);
  345. }
  346. Exception CreateTypeException (Type type)
  347. {
  348. return new NotSupportedException ("The type " + type.FullName + " may not be serialized with SOAP-encoded messages. Set the Use for your message to Literal");
  349. }
  350. Exception CreateStructException (Type type)
  351. {
  352. return new NotSupportedException ("Cannot serialize " + type.FullName + ". Nested structs are not supported with encoded SOAP");
  353. }
  354. #endregion // Methods
  355. }
  356. }