XmlSerializerOperationBehavior.cs 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Description
  5. {
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Reflection;
  11. using System.Runtime;
  12. using System.Security;
  13. using System.ServiceModel;
  14. using System.ServiceModel.Channels;
  15. using System.ServiceModel.Dispatcher;
  16. using System.Xml;
  17. using System.Xml.Serialization;
  18. public class XmlSerializerOperationBehavior : IOperationBehavior, IWsdlExportExtension
  19. {
  20. readonly Reflector.OperationReflector reflector;
  21. readonly bool builtInOperationBehavior;
  22. public XmlSerializerOperationBehavior(OperationDescription operation)
  23. : this(operation, null)
  24. {
  25. }
  26. public XmlSerializerOperationBehavior(OperationDescription operation, XmlSerializerFormatAttribute attribute)
  27. {
  28. if (operation == null)
  29. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("operation");
  30. #pragma warning suppress 56506 // Declaring contract cannot be null
  31. Reflector parentReflector = new Reflector(operation.DeclaringContract.Namespace, operation.DeclaringContract.ContractType);
  32. #pragma warning suppress 56506 // parentReflector cannot be null
  33. this.reflector = parentReflector.ReflectOperation(operation, attribute ?? new XmlSerializerFormatAttribute());
  34. }
  35. internal XmlSerializerOperationBehavior(OperationDescription operation, XmlSerializerFormatAttribute attribute, Reflector parentReflector)
  36. : this(operation, attribute)
  37. {
  38. // used by System.ServiceModel.Web
  39. this.reflector = parentReflector.ReflectOperation(operation, attribute ?? new XmlSerializerFormatAttribute());
  40. }
  41. XmlSerializerOperationBehavior(Reflector.OperationReflector reflector, bool builtInOperationBehavior)
  42. {
  43. Fx.Assert(reflector != null, "");
  44. this.reflector = reflector;
  45. this.builtInOperationBehavior = builtInOperationBehavior;
  46. }
  47. internal Reflector.OperationReflector OperationReflector
  48. {
  49. get { return this.reflector; }
  50. }
  51. internal bool IsBuiltInOperationBehavior
  52. {
  53. get { return this.builtInOperationBehavior; }
  54. }
  55. public XmlSerializerFormatAttribute XmlSerializerFormatAttribute
  56. {
  57. get
  58. {
  59. return this.reflector.Attribute;
  60. }
  61. }
  62. internal static XmlSerializerOperationFormatter CreateOperationFormatter(OperationDescription operation)
  63. {
  64. return new XmlSerializerOperationBehavior(operation).CreateFormatter();
  65. }
  66. internal static XmlSerializerOperationFormatter CreateOperationFormatter(OperationDescription operation, XmlSerializerFormatAttribute attr)
  67. {
  68. return new XmlSerializerOperationBehavior(operation, attr).CreateFormatter();
  69. }
  70. internal static void AddBehaviors(ContractDescription contract)
  71. {
  72. AddBehaviors(contract, false);
  73. }
  74. internal static void AddBuiltInBehaviors(ContractDescription contract)
  75. {
  76. AddBehaviors(contract, true);
  77. }
  78. static void AddBehaviors(ContractDescription contract, bool builtInOperationBehavior)
  79. {
  80. Reflector reflector = new Reflector(contract.Namespace, contract.ContractType);
  81. foreach (OperationDescription operation in contract.Operations)
  82. {
  83. Reflector.OperationReflector operationReflector = reflector.ReflectOperation(operation);
  84. if (operationReflector != null)
  85. {
  86. bool isInherited = operation.DeclaringContract != contract;
  87. if (!isInherited)
  88. {
  89. operation.Behaviors.Add(new XmlSerializerOperationBehavior(operationReflector, builtInOperationBehavior));
  90. operation.Behaviors.Add(new XmlSerializerOperationGenerator(new XmlSerializerImportOptions()));
  91. }
  92. }
  93. }
  94. }
  95. internal XmlSerializerOperationFormatter CreateFormatter()
  96. {
  97. return new XmlSerializerOperationFormatter(reflector.Operation, reflector.Attribute, reflector.Request, reflector.Reply);
  98. }
  99. XmlSerializerFaultFormatter CreateFaultFormatter(SynchronizedCollection<FaultContractInfo> faultContractInfos)
  100. {
  101. return new XmlSerializerFaultFormatter(faultContractInfos, reflector.XmlSerializerFaultContractInfos);
  102. }
  103. void IOperationBehavior.Validate(OperationDescription description)
  104. {
  105. }
  106. void IOperationBehavior.AddBindingParameters(OperationDescription description, BindingParameterCollection parameters)
  107. {
  108. }
  109. void IOperationBehavior.ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch)
  110. {
  111. if (description == null)
  112. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("description");
  113. if (dispatch == null)
  114. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("dispatch");
  115. if (dispatch.Formatter == null)
  116. {
  117. dispatch.Formatter = (IDispatchMessageFormatter)CreateFormatter();
  118. dispatch.DeserializeRequest = reflector.RequestRequiresSerialization;
  119. dispatch.SerializeReply = reflector.ReplyRequiresSerialization;
  120. }
  121. if (reflector.Attribute.SupportFaults)
  122. {
  123. if (!dispatch.IsFaultFormatterSetExplicit)
  124. {
  125. dispatch.FaultFormatter = (IDispatchFaultFormatter)CreateFaultFormatter(dispatch.FaultContractInfos);
  126. }
  127. else
  128. {
  129. var wrapper = dispatch.FaultFormatter as IDispatchFaultFormatterWrapper;
  130. if (wrapper != null)
  131. {
  132. wrapper.InnerFaultFormatter = (IDispatchFaultFormatter)CreateFaultFormatter(dispatch.FaultContractInfos);
  133. }
  134. }
  135. }
  136. }
  137. void IOperationBehavior.ApplyClientBehavior(OperationDescription description, ClientOperation proxy)
  138. {
  139. if (description == null)
  140. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("description");
  141. if (proxy == null)
  142. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("proxy");
  143. if (proxy.Formatter == null)
  144. {
  145. proxy.Formatter = (IClientMessageFormatter)CreateFormatter();
  146. proxy.SerializeRequest = reflector.RequestRequiresSerialization;
  147. proxy.DeserializeReply = reflector.ReplyRequiresSerialization;
  148. }
  149. if (reflector.Attribute.SupportFaults && !proxy.IsFaultFormatterSetExplicit)
  150. proxy.FaultFormatter = (IClientFaultFormatter)CreateFaultFormatter(proxy.FaultContractInfos);
  151. }
  152. void IWsdlExportExtension.ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext endpointContext)
  153. {
  154. if (exporter == null)
  155. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("exporter");
  156. if (endpointContext == null)
  157. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("endpointContext");
  158. MessageContractExporter.ExportMessageBinding(exporter, endpointContext, typeof(XmlSerializerMessageContractExporter), this.reflector.Operation);
  159. }
  160. void IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext contractContext)
  161. {
  162. if (exporter == null)
  163. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("exporter");
  164. if (contractContext == null)
  165. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contractContext");
  166. new XmlSerializerMessageContractExporter(exporter, contractContext, this.reflector.Operation, this).ExportMessageContract();
  167. }
  168. public Collection<XmlMapping> GetXmlMappings()
  169. {
  170. Collection<XmlMapping> mappings = new Collection<XmlMapping>();
  171. if (OperationReflector.Request != null && OperationReflector.Request.HeadersMapping != null)
  172. mappings.Add(OperationReflector.Request.HeadersMapping);
  173. if (OperationReflector.Request != null && OperationReflector.Request.BodyMapping != null)
  174. mappings.Add(OperationReflector.Request.BodyMapping);
  175. if (OperationReflector.Reply != null && OperationReflector.Reply.HeadersMapping != null)
  176. mappings.Add(OperationReflector.Reply.HeadersMapping);
  177. if (OperationReflector.Reply != null && OperationReflector.Reply.BodyMapping != null)
  178. mappings.Add(OperationReflector.Reply.BodyMapping);
  179. return mappings;
  180. }
  181. // helper for reflecting operations
  182. internal class Reflector
  183. {
  184. readonly XmlSerializerImporter importer;
  185. readonly SerializerGenerationContext generation;
  186. Collection<OperationReflector> operationReflectors = new Collection<OperationReflector>();
  187. object thisLock = new object();
  188. internal Reflector(string defaultNs, Type type)
  189. {
  190. this.importer = new XmlSerializerImporter(defaultNs);
  191. this.generation = new SerializerGenerationContext(type);
  192. }
  193. internal void EnsureMessageInfos()
  194. {
  195. lock (this.thisLock)
  196. {
  197. foreach (OperationReflector operationReflector in operationReflectors)
  198. {
  199. operationReflector.EnsureMessageInfos();
  200. }
  201. }
  202. }
  203. static XmlSerializerFormatAttribute FindAttribute(OperationDescription operation)
  204. {
  205. Type contractType = operation.DeclaringContract != null ? operation.DeclaringContract.ContractType : null;
  206. XmlSerializerFormatAttribute contractFormatAttribute = contractType != null ? TypeLoader.GetFormattingAttribute(contractType, null) as XmlSerializerFormatAttribute : null;
  207. return TypeLoader.GetFormattingAttribute(operation.OperationMethod, contractFormatAttribute) as XmlSerializerFormatAttribute;
  208. }
  209. // auto-reflects the operation, returning null if no attribute was found or inherited
  210. internal OperationReflector ReflectOperation(OperationDescription operation)
  211. {
  212. XmlSerializerFormatAttribute attr = FindAttribute(operation);
  213. if (attr == null)
  214. return null;
  215. return ReflectOperation(operation, attr);
  216. }
  217. // overrides the auto-reflection with an attribute
  218. internal OperationReflector ReflectOperation(OperationDescription operation, XmlSerializerFormatAttribute attrOverride)
  219. {
  220. OperationReflector operationReflector = new OperationReflector(this, operation, attrOverride, true/*reflectOnDemand*/);
  221. operationReflectors.Add(operationReflector);
  222. return operationReflector;
  223. }
  224. internal class OperationReflector
  225. {
  226. readonly Reflector parent;
  227. internal readonly OperationDescription Operation;
  228. internal readonly XmlSerializerFormatAttribute Attribute;
  229. internal readonly bool IsEncoded;
  230. internal readonly bool IsRpc;
  231. internal readonly bool IsOneWay;
  232. internal readonly bool RequestRequiresSerialization;
  233. internal readonly bool ReplyRequiresSerialization;
  234. readonly string keyBase;
  235. MessageInfo request;
  236. MessageInfo reply;
  237. SynchronizedCollection<XmlSerializerFaultContractInfo> xmlSerializerFaultContractInfos;
  238. internal OperationReflector(Reflector parent, OperationDescription operation, XmlSerializerFormatAttribute attr, bool reflectOnDemand)
  239. {
  240. Fx.Assert(parent != null, "");
  241. Fx.Assert(operation != null, "");
  242. Fx.Assert(attr != null, "");
  243. OperationFormatter.Validate(operation, attr.Style == OperationFormatStyle.Rpc, attr.IsEncoded);
  244. this.parent = parent;
  245. this.Operation = operation;
  246. this.Attribute = attr;
  247. this.IsEncoded = attr.IsEncoded;
  248. this.IsRpc = (attr.Style == OperationFormatStyle.Rpc);
  249. this.IsOneWay = operation.Messages.Count == 1;
  250. this.RequestRequiresSerialization = !operation.Messages[0].IsUntypedMessage;
  251. this.ReplyRequiresSerialization = !this.IsOneWay && !operation.Messages[1].IsUntypedMessage;
  252. MethodInfo methodInfo = operation.OperationMethod;
  253. if (methodInfo == null)
  254. {
  255. // keyBase needs to be unique within the scope of the parent reflector
  256. keyBase = string.Empty;
  257. if (operation.DeclaringContract != null)
  258. {
  259. keyBase = operation.DeclaringContract.Name + "," + operation.DeclaringContract.Namespace + ":";
  260. }
  261. keyBase = keyBase + operation.Name;
  262. }
  263. else
  264. keyBase = methodInfo.DeclaringType.FullName + ":" + methodInfo.ToString();
  265. foreach (MessageDescription message in operation.Messages)
  266. foreach (MessageHeaderDescription header in message.Headers)
  267. SetUnknownHeaderInDescription(header);
  268. if (!reflectOnDemand)
  269. {
  270. this.EnsureMessageInfos();
  271. }
  272. }
  273. private void SetUnknownHeaderInDescription(MessageHeaderDescription header)
  274. {
  275. if (this.IsEncoded) //XmlAnyElementAttribute does not apply
  276. return;
  277. if (header.AdditionalAttributesProvider != null)
  278. {
  279. XmlAttributes xmlAttributes = new XmlAttributes(header.AdditionalAttributesProvider);
  280. foreach (XmlAnyElementAttribute anyElement in xmlAttributes.XmlAnyElements)
  281. {
  282. if (String.IsNullOrEmpty(anyElement.Name))
  283. {
  284. header.IsUnknownHeaderCollection = true;
  285. }
  286. }
  287. }
  288. }
  289. string ContractName
  290. {
  291. get { return this.Operation.DeclaringContract.Name; }
  292. }
  293. string ContractNamespace
  294. {
  295. get { return this.Operation.DeclaringContract.Namespace; }
  296. }
  297. internal MessageInfo Request
  298. {
  299. get
  300. {
  301. parent.EnsureMessageInfos();
  302. return this.request;
  303. }
  304. }
  305. internal MessageInfo Reply
  306. {
  307. get
  308. {
  309. parent.EnsureMessageInfos();
  310. return this.reply;
  311. }
  312. }
  313. internal SynchronizedCollection<XmlSerializerFaultContractInfo> XmlSerializerFaultContractInfos
  314. {
  315. get
  316. {
  317. parent.EnsureMessageInfos();
  318. return this.xmlSerializerFaultContractInfos;
  319. }
  320. }
  321. internal void EnsureMessageInfos()
  322. {
  323. if (this.request == null)
  324. {
  325. foreach (Type knownType in Operation.KnownTypes)
  326. {
  327. if (knownType == null)
  328. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxKnownTypeNull, Operation.Name)));
  329. parent.importer.IncludeType(knownType, IsEncoded);
  330. }
  331. this.request = CreateMessageInfo(this.Operation.Messages[0], ":Request");
  332. if (this.request != null && this.IsRpc && this.Operation.IsValidateRpcWrapperName && this.request.BodyMapping.XsdElementName != this.Operation.Name)
  333. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxRpcMessageBodyPartNameInvalid, Operation.Name, this.Operation.Messages[0].MessageName, request.BodyMapping.XsdElementName, this.Operation.Name)));
  334. if (!this.IsOneWay)
  335. {
  336. this.reply = CreateMessageInfo(this.Operation.Messages[1], ":Response");
  337. XmlName responseName = TypeLoader.GetBodyWrapperResponseName(this.Operation.Name);
  338. if (this.reply != null && this.IsRpc && this.Operation.IsValidateRpcWrapperName && this.reply.BodyMapping.XsdElementName != responseName.EncodedName)
  339. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxRpcMessageBodyPartNameInvalid, Operation.Name, this.Operation.Messages[1].MessageName, reply.BodyMapping.XsdElementName, responseName.EncodedName)));
  340. }
  341. if (this.Attribute.SupportFaults)
  342. {
  343. GenerateXmlSerializerFaultContractInfos();
  344. }
  345. }
  346. }
  347. void GenerateXmlSerializerFaultContractInfos()
  348. {
  349. SynchronizedCollection<XmlSerializerFaultContractInfo> faultInfos = new SynchronizedCollection<XmlSerializerFaultContractInfo>();
  350. for (int i = 0; i < this.Operation.Faults.Count; i++)
  351. {
  352. FaultDescription fault = this.Operation.Faults[i];
  353. FaultContractInfo faultContractInfo = new FaultContractInfo(fault.Action, fault.DetailType, fault.ElementName, fault.Namespace, this.Operation.KnownTypes);
  354. XmlQualifiedName elementName;
  355. XmlMembersMapping xmlMembersMapping = this.ImportFaultElement(fault, out elementName);
  356. SerializerStub serializerStub = parent.generation.AddSerializer(xmlMembersMapping);
  357. faultInfos.Add(new XmlSerializerFaultContractInfo(faultContractInfo, serializerStub, elementName));
  358. }
  359. this.xmlSerializerFaultContractInfos = faultInfos;
  360. }
  361. MessageInfo CreateMessageInfo(MessageDescription message, string key)
  362. {
  363. if (message.IsUntypedMessage)
  364. return null;
  365. MessageInfo info = new MessageInfo();
  366. if (message.IsTypedMessage)
  367. key = message.MessageType.FullName + ":" + IsEncoded + ":" + IsRpc;
  368. XmlMembersMapping headersMapping = LoadHeadersMapping(message, key + ":Headers");
  369. info.SetHeaders(parent.generation.AddSerializer(headersMapping));
  370. MessagePartDescriptionCollection rpcEncodedTypedMessgeBodyParts;
  371. info.SetBody(parent.generation.AddSerializer(LoadBodyMapping(message, key, out rpcEncodedTypedMessgeBodyParts)), rpcEncodedTypedMessgeBodyParts);
  372. CreateHeaderDescriptionTable(message, info, headersMapping);
  373. return info;
  374. }
  375. private void CreateHeaderDescriptionTable(MessageDescription message, MessageInfo info, XmlMembersMapping headersMapping)
  376. {
  377. int headerNameIndex = 0;
  378. OperationFormatter.MessageHeaderDescriptionTable headerDescriptionTable = new OperationFormatter.MessageHeaderDescriptionTable();
  379. info.SetHeaderDescriptionTable(headerDescriptionTable);
  380. foreach (MessageHeaderDescription header in message.Headers)
  381. {
  382. if (header.IsUnknownHeaderCollection)
  383. info.SetUnknownHeaderDescription(header);
  384. else if (headersMapping != null)
  385. {
  386. XmlMemberMapping memberMapping = headersMapping[headerNameIndex++];
  387. string headerName, headerNs;
  388. if (IsEncoded)
  389. {
  390. headerName = memberMapping.TypeName;
  391. headerNs = memberMapping.TypeNamespace;
  392. }
  393. else
  394. {
  395. headerName = memberMapping.XsdElementName;
  396. headerNs = memberMapping.Namespace;
  397. }
  398. if (headerName != header.Name)
  399. {
  400. if (message.MessageType != null)
  401. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxHeaderNameMismatchInMessageContract, message.MessageType, header.MemberInfo.Name, header.Name, headerName)));
  402. else
  403. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxHeaderNameMismatchInOperation, this.Operation.Name, this.Operation.DeclaringContract.Name, this.Operation.DeclaringContract.Namespace, header.Name, headerName)));
  404. }
  405. if (headerNs != header.Namespace)
  406. {
  407. if (message.MessageType != null)
  408. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxHeaderNamespaceMismatchInMessageContract, message.MessageType, header.MemberInfo.Name, header.Namespace, headerNs)));
  409. else
  410. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxHeaderNamespaceMismatchInOperation, this.Operation.Name, this.Operation.DeclaringContract.Name, this.Operation.DeclaringContract.Namespace, header.Namespace, headerNs)));
  411. }
  412. headerDescriptionTable.Add(headerName, headerNs, header);
  413. }
  414. }
  415. }
  416. XmlMembersMapping LoadBodyMapping(MessageDescription message, string mappingKey, out MessagePartDescriptionCollection rpcEncodedTypedMessageBodyParts)
  417. {
  418. MessagePartDescription returnPart;
  419. string wrapperName, wrapperNs;
  420. MessagePartDescriptionCollection bodyParts;
  421. if (IsEncoded && message.IsTypedMessage && message.Body.WrapperName == null)
  422. {
  423. MessagePartDescription wrapperPart = GetWrapperPart(message);
  424. returnPart = null;
  425. rpcEncodedTypedMessageBodyParts = bodyParts = GetWrappedParts(wrapperPart);
  426. wrapperName = wrapperPart.Name;
  427. wrapperNs = wrapperPart.Namespace;
  428. }
  429. else
  430. {
  431. rpcEncodedTypedMessageBodyParts = null;
  432. returnPart = OperationFormatter.IsValidReturnValue(message.Body.ReturnValue) ? message.Body.ReturnValue : null;
  433. bodyParts = message.Body.Parts;
  434. wrapperName = message.Body.WrapperName;
  435. wrapperNs = message.Body.WrapperNamespace;
  436. }
  437. bool isWrapped = (wrapperName != null);
  438. bool hasReturnValue = returnPart != null;
  439. int paramCount = bodyParts.Count + (hasReturnValue ? 1 : 0);
  440. if (paramCount == 0 && !isWrapped) // no need to create serializer
  441. {
  442. return null;
  443. }
  444. XmlReflectionMember[] members = new XmlReflectionMember[paramCount];
  445. int paramIndex = 0;
  446. if (hasReturnValue)
  447. members[paramIndex++] = XmlSerializerHelper.GetXmlReflectionMember(returnPart, IsRpc, IsEncoded, isWrapped);
  448. for (int i = 0; i < bodyParts.Count; i++)
  449. members[paramIndex++] = XmlSerializerHelper.GetXmlReflectionMember(bodyParts[i], IsRpc, IsEncoded, isWrapped);
  450. if (!isWrapped)
  451. wrapperNs = ContractNamespace;
  452. return ImportMembersMapping(wrapperName, wrapperNs, members, isWrapped, IsRpc, mappingKey);
  453. }
  454. private MessagePartDescription GetWrapperPart(MessageDescription message)
  455. {
  456. if (message.Body.Parts.Count != 1)
  457. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxRpcMessageMustHaveASingleBody, Operation.Name, message.MessageName)));
  458. MessagePartDescription bodyPart = message.Body.Parts[0];
  459. Type bodyObjectType = bodyPart.Type;
  460. if (bodyObjectType.BaseType != null && bodyObjectType.BaseType != typeof(object))
  461. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxBodyObjectTypeCannotBeInherited, bodyObjectType.FullName)));
  462. if (typeof(IEnumerable).IsAssignableFrom(bodyObjectType))
  463. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxBodyObjectTypeCannotBeInterface, bodyObjectType.FullName, typeof(IEnumerable).FullName)));
  464. if (typeof(IXmlSerializable).IsAssignableFrom(bodyObjectType))
  465. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxBodyObjectTypeCannotBeInterface, bodyObjectType.FullName, typeof(IXmlSerializable).FullName)));
  466. return bodyPart;
  467. }
  468. private MessagePartDescriptionCollection GetWrappedParts(MessagePartDescription bodyPart)
  469. {
  470. Type bodyObjectType = bodyPart.Type;
  471. MessagePartDescriptionCollection partList = new MessagePartDescriptionCollection();
  472. foreach (MemberInfo member in bodyObjectType.GetMembers(BindingFlags.Instance | BindingFlags.Public))
  473. {
  474. if ((member.MemberType & (MemberTypes.Field | MemberTypes.Property)) == 0)
  475. continue;
  476. if (member.IsDefined(typeof(SoapIgnoreAttribute), false/*inherit*/))
  477. continue;
  478. XmlName xmlName = new XmlName(member.Name);
  479. MessagePartDescription part = new MessagePartDescription(xmlName.EncodedName, string.Empty);
  480. part.AdditionalAttributesProvider = part.MemberInfo = member;
  481. part.Index = part.SerializationPosition = partList.Count;
  482. part.Type = (member.MemberType == MemberTypes.Property) ? ((PropertyInfo)member).PropertyType : ((FieldInfo)member).FieldType;
  483. if (bodyPart.HasProtectionLevel)
  484. part.ProtectionLevel = bodyPart.ProtectionLevel;
  485. partList.Add(part);
  486. }
  487. return partList;
  488. }
  489. XmlMembersMapping LoadHeadersMapping(MessageDescription message, string mappingKey)
  490. {
  491. int headerCount = message.Headers.Count;
  492. if (headerCount == 0)
  493. return null;
  494. if (IsEncoded)
  495. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxHeadersAreNotSupportedInEncoded, message.MessageName)));
  496. int unknownHeaderCount = 0, headerIndex = 0;
  497. XmlReflectionMember[] members = new XmlReflectionMember[headerCount];
  498. for (int i = 0; i < headerCount; i++)
  499. {
  500. MessageHeaderDescription header = message.Headers[i];
  501. if (!header.IsUnknownHeaderCollection)
  502. {
  503. members[headerIndex++] = XmlSerializerHelper.GetXmlReflectionMember(header, false/*isRpc*/, IsEncoded, false/*isWrapped*/);
  504. }
  505. else
  506. {
  507. unknownHeaderCount++;
  508. }
  509. }
  510. if (unknownHeaderCount == headerCount)
  511. {
  512. return null;
  513. }
  514. if (unknownHeaderCount > 0)
  515. {
  516. XmlReflectionMember[] newMembers = new XmlReflectionMember[headerCount - unknownHeaderCount];
  517. Array.Copy(members, newMembers, newMembers.Length);
  518. members = newMembers;
  519. }
  520. return ImportMembersMapping(ContractName, ContractNamespace, members, false /*isWrapped*/, false /*isRpc*/, mappingKey);
  521. }
  522. internal XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool rpc, string mappingKey)
  523. {
  524. string key = mappingKey.StartsWith(":", StringComparison.Ordinal) ? keyBase + mappingKey : mappingKey;
  525. return this.parent.importer.ImportMembersMapping(new XmlName(elementName, true /*isEncoded*/), ns, members, hasWrapperElement, rpc, this.IsEncoded, key);
  526. }
  527. internal XmlMembersMapping ImportFaultElement(FaultDescription fault, out XmlQualifiedName elementName)
  528. {
  529. // the number of reflection members is always 1 because there is only one fault detail type
  530. XmlReflectionMember[] members = new XmlReflectionMember[1];
  531. XmlName faultElementName = fault.ElementName;
  532. string faultNamespace = fault.Namespace;
  533. if (faultElementName == null)
  534. {
  535. XmlTypeMapping mapping = this.parent.importer.ImportTypeMapping(fault.DetailType, this.IsEncoded);
  536. faultElementName = new XmlName(mapping.ElementName, this.IsEncoded);
  537. faultNamespace = mapping.Namespace;
  538. if (faultElementName == null)
  539. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxFaultTypeAnonymous, this.Operation.Name, fault.DetailType.FullName)));
  540. }
  541. elementName = new XmlQualifiedName(faultElementName.DecodedName, faultNamespace);
  542. members[0] = XmlSerializerHelper.GetXmlReflectionMember(null /*memberName*/, faultElementName, faultNamespace, fault.DetailType,
  543. null /*additionalAttributesProvider*/, false /*isMultiple*/, this.IsEncoded, false /*isWrapped*/);
  544. string mappingKey = "fault:" + faultElementName.DecodedName + ":" + faultNamespace;
  545. return ImportMembersMapping(faultElementName.EncodedName, faultNamespace, members, false /*hasWrapperElement*/, this.IsRpc, mappingKey);
  546. }
  547. }
  548. class XmlSerializerImporter
  549. {
  550. readonly string defaultNs;
  551. XmlReflectionImporter xmlImporter;
  552. SoapReflectionImporter soapImporter;
  553. Dictionary<string, XmlMembersMapping> xmlMappings;
  554. internal XmlSerializerImporter(string defaultNs)
  555. {
  556. this.defaultNs = defaultNs;
  557. this.xmlImporter = null;
  558. this.soapImporter = null;
  559. }
  560. SoapReflectionImporter SoapImporter
  561. {
  562. get
  563. {
  564. if (this.soapImporter == null)
  565. {
  566. this.soapImporter = new SoapReflectionImporter(NamingHelper.CombineUriStrings(defaultNs, "encoded"));
  567. }
  568. return this.soapImporter;
  569. }
  570. }
  571. XmlReflectionImporter XmlImporter
  572. {
  573. get
  574. {
  575. if (this.xmlImporter == null)
  576. {
  577. this.xmlImporter = new XmlReflectionImporter(defaultNs);
  578. }
  579. return this.xmlImporter;
  580. }
  581. }
  582. Dictionary<string, XmlMembersMapping> XmlMappings
  583. {
  584. get
  585. {
  586. if (this.xmlMappings == null)
  587. {
  588. this.xmlMappings = new Dictionary<string, XmlMembersMapping>();
  589. }
  590. return this.xmlMappings;
  591. }
  592. }
  593. internal XmlMembersMapping ImportMembersMapping(XmlName elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool rpc, bool isEncoded, string mappingKey)
  594. {
  595. XmlMembersMapping mapping;
  596. string mappingName = elementName.DecodedName;
  597. if (XmlMappings.TryGetValue(mappingKey, out mapping))
  598. {
  599. return mapping;
  600. }
  601. if (isEncoded)
  602. mapping = this.SoapImporter.ImportMembersMapping(mappingName, ns, members, hasWrapperElement, rpc);
  603. else
  604. mapping = this.XmlImporter.ImportMembersMapping(mappingName, ns, members, hasWrapperElement, rpc);
  605. mapping.SetKey(mappingKey);
  606. XmlMappings.Add(mappingKey, mapping);
  607. return mapping;
  608. }
  609. internal XmlTypeMapping ImportTypeMapping(Type type, bool isEncoded)
  610. {
  611. if (isEncoded)
  612. return this.SoapImporter.ImportTypeMapping(type);
  613. else
  614. return this.XmlImporter.ImportTypeMapping(type);
  615. }
  616. internal void IncludeType(Type knownType, bool isEncoded)
  617. {
  618. if (isEncoded)
  619. this.SoapImporter.IncludeType(knownType);
  620. else
  621. this.XmlImporter.IncludeType(knownType);
  622. }
  623. }
  624. internal class SerializerGenerationContext
  625. {
  626. List<XmlMembersMapping> Mappings = new List<XmlMembersMapping>();
  627. XmlSerializer[] serializers = null;
  628. Type type;
  629. object thisLock = new object();
  630. internal SerializerGenerationContext(Type type)
  631. {
  632. this.type = type;
  633. }
  634. // returns a stub to a serializer
  635. internal SerializerStub AddSerializer(XmlMembersMapping mapping)
  636. {
  637. int handle = -1;
  638. if (mapping != null)
  639. {
  640. handle = ((IList)Mappings).Add(mapping);
  641. }
  642. return new SerializerStub(this, mapping, handle);
  643. }
  644. internal XmlSerializer GetSerializer(int handle)
  645. {
  646. if (handle < 0)
  647. {
  648. return null;
  649. }
  650. if (this.serializers == null)
  651. {
  652. lock (this.thisLock)
  653. {
  654. if (this.serializers == null)
  655. {
  656. this.serializers = GenerateSerializers();
  657. }
  658. }
  659. }
  660. return this.serializers[handle];
  661. }
  662. XmlSerializer[] GenerateSerializers()
  663. {
  664. //this.Mappings may have duplicate mappings (for e.g. samed message contract is used by more than one operation)
  665. //XmlSerializer.FromMappings require unique mappings. The following code uniquifies, calls FromMappings and deuniquifies
  666. List<XmlMembersMapping> uniqueMappings = new List<XmlMembersMapping>();
  667. int[] uniqueIndexes = new int[Mappings.Count];
  668. for (int srcIndex = 0; srcIndex < Mappings.Count; srcIndex++)
  669. {
  670. XmlMembersMapping mapping = Mappings[srcIndex];
  671. int uniqueIndex = uniqueMappings.IndexOf(mapping);
  672. if (uniqueIndex < 0)
  673. {
  674. uniqueMappings.Add(mapping);
  675. uniqueIndex = uniqueMappings.Count - 1;
  676. }
  677. uniqueIndexes[srcIndex] = uniqueIndex;
  678. }
  679. XmlSerializer[] uniqueSerializers = CreateSerializersFromMappings(uniqueMappings.ToArray(), type);
  680. if (uniqueMappings.Count == Mappings.Count)
  681. return uniqueSerializers;
  682. XmlSerializer[] serializers = new XmlSerializer[Mappings.Count];
  683. for (int i = 0; i < Mappings.Count; i++)
  684. {
  685. serializers[i] = uniqueSerializers[uniqueIndexes[i]];
  686. }
  687. return serializers;
  688. }
  689. [Fx.Tag.SecurityNote(Critical = "XmlSerializer.FromMappings has a LinkDemand.",
  690. Safe = "LinkDemand is spurious, not protecting anything in particular.")]
  691. [SecuritySafeCritical]
  692. XmlSerializer[] CreateSerializersFromMappings(XmlMapping[] mappings, Type type)
  693. {
  694. return XmlSerializer.FromMappings(mappings, type);
  695. }
  696. }
  697. internal struct SerializerStub
  698. {
  699. readonly SerializerGenerationContext context;
  700. internal readonly XmlMembersMapping Mapping;
  701. internal readonly int Handle;
  702. internal SerializerStub(SerializerGenerationContext context, XmlMembersMapping mapping, int handle)
  703. {
  704. this.context = context;
  705. this.Mapping = mapping;
  706. this.Handle = handle;
  707. }
  708. internal XmlSerializer GetSerializer()
  709. {
  710. return context.GetSerializer(Handle);
  711. }
  712. }
  713. internal class XmlSerializerFaultContractInfo
  714. {
  715. FaultContractInfo faultContractInfo;
  716. SerializerStub serializerStub;
  717. XmlQualifiedName faultContractElementName;
  718. XmlSerializerObjectSerializer serializer;
  719. internal XmlSerializerFaultContractInfo(FaultContractInfo faultContractInfo, SerializerStub serializerStub,
  720. XmlQualifiedName faultContractElementName)
  721. {
  722. if (faultContractInfo == null)
  723. {
  724. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("faultContractInfo");
  725. }
  726. if (faultContractElementName == null)
  727. {
  728. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("faultContractElementName");
  729. }
  730. this.faultContractInfo = faultContractInfo;
  731. this.serializerStub = serializerStub;
  732. this.faultContractElementName = faultContractElementName;
  733. }
  734. internal FaultContractInfo FaultContractInfo
  735. {
  736. get { return this.faultContractInfo; }
  737. }
  738. internal XmlQualifiedName FaultContractElementName
  739. {
  740. get { return this.faultContractElementName; }
  741. }
  742. internal XmlSerializerObjectSerializer Serializer
  743. {
  744. get
  745. {
  746. if (this.serializer == null)
  747. this.serializer = new XmlSerializerObjectSerializer(faultContractInfo.Detail, this.faultContractElementName, this.serializerStub.GetSerializer());
  748. return this.serializer;
  749. }
  750. }
  751. }
  752. internal class MessageInfo : XmlSerializerOperationFormatter.MessageInfo
  753. {
  754. SerializerStub headers;
  755. SerializerStub body;
  756. OperationFormatter.MessageHeaderDescriptionTable headerDescriptionTable;
  757. MessageHeaderDescription unknownHeaderDescription;
  758. MessagePartDescriptionCollection rpcEncodedTypedMessageBodyParts;
  759. internal XmlMembersMapping BodyMapping
  760. {
  761. get { return body.Mapping; }
  762. }
  763. internal override XmlSerializer BodySerializer
  764. {
  765. get { return body.GetSerializer(); }
  766. }
  767. internal XmlMembersMapping HeadersMapping
  768. {
  769. get { return headers.Mapping; }
  770. }
  771. internal override XmlSerializer HeaderSerializer
  772. {
  773. get { return headers.GetSerializer(); }
  774. }
  775. internal override OperationFormatter.MessageHeaderDescriptionTable HeaderDescriptionTable
  776. {
  777. get { return this.headerDescriptionTable; }
  778. }
  779. internal override MessageHeaderDescription UnknownHeaderDescription
  780. {
  781. get { return this.unknownHeaderDescription; }
  782. }
  783. internal override MessagePartDescriptionCollection RpcEncodedTypedMessageBodyParts
  784. {
  785. get { return rpcEncodedTypedMessageBodyParts; }
  786. }
  787. internal void SetBody(SerializerStub body, MessagePartDescriptionCollection rpcEncodedTypedMessageBodyParts)
  788. {
  789. this.body = body;
  790. this.rpcEncodedTypedMessageBodyParts = rpcEncodedTypedMessageBodyParts;
  791. }
  792. internal void SetHeaders(SerializerStub headers)
  793. {
  794. this.headers = headers;
  795. }
  796. internal void SetHeaderDescriptionTable(OperationFormatter.MessageHeaderDescriptionTable headerDescriptionTable)
  797. {
  798. this.headerDescriptionTable = headerDescriptionTable;
  799. }
  800. internal void SetUnknownHeaderDescription(MessageHeaderDescription unknownHeaderDescription)
  801. {
  802. this.unknownHeaderDescription = unknownHeaderDescription;
  803. }
  804. }
  805. }
  806. }
  807. static class XmlSerializerHelper
  808. {
  809. static internal XmlReflectionMember GetXmlReflectionMember(MessagePartDescription part, bool isRpc, bool isEncoded, bool isWrapped)
  810. {
  811. string ns = isRpc ? null : part.Namespace;
  812. ICustomAttributeProvider additionalAttributesProvider = null;
  813. if (isEncoded || part.AdditionalAttributesProvider is MemberInfo)
  814. additionalAttributesProvider = part.AdditionalAttributesProvider;
  815. XmlName memberName = string.IsNullOrEmpty(part.UniquePartName) ? null : new XmlName(part.UniquePartName, true /*isEncoded*/);
  816. XmlName elementName = part.XmlName;
  817. return GetXmlReflectionMember(memberName, elementName, ns, part.Type, additionalAttributesProvider, part.Multiple, isEncoded, isWrapped);
  818. }
  819. static internal XmlReflectionMember GetXmlReflectionMember(XmlName memberName, XmlName elementName, string ns, Type type, ICustomAttributeProvider additionalAttributesProvider, bool isMultiple, bool isEncoded, bool isWrapped)
  820. {
  821. if (isEncoded && isMultiple)
  822. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxMultiplePartsNotAllowedInEncoded, elementName.DecodedName, ns)));
  823. XmlReflectionMember member = new XmlReflectionMember();
  824. member.MemberName = (memberName ?? elementName).DecodedName;
  825. member.MemberType = type;
  826. if (member.MemberType.IsByRef)
  827. member.MemberType = member.MemberType.GetElementType();
  828. if (isMultiple)
  829. member.MemberType = member.MemberType.MakeArrayType();
  830. if (additionalAttributesProvider != null)
  831. {
  832. if (isEncoded)
  833. member.SoapAttributes = new SoapAttributes(additionalAttributesProvider);
  834. else
  835. member.XmlAttributes = new XmlAttributes(additionalAttributesProvider);
  836. }
  837. if (isEncoded)
  838. {
  839. if (member.SoapAttributes == null)
  840. member.SoapAttributes = new SoapAttributes();
  841. else
  842. {
  843. Type invalidAttributeType = null;
  844. if (member.SoapAttributes.SoapAttribute != null)
  845. invalidAttributeType = typeof(SoapAttributeAttribute);
  846. else if (member.SoapAttributes.SoapIgnore)
  847. invalidAttributeType = typeof(SoapIgnoreAttribute);
  848. else if (member.SoapAttributes.SoapType != null)
  849. invalidAttributeType = typeof(SoapTypeAttribute);
  850. if (invalidAttributeType != null)
  851. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxInvalidSoapAttribute, invalidAttributeType, elementName.DecodedName)));
  852. }
  853. if (member.SoapAttributes.SoapElement == null)
  854. member.SoapAttributes.SoapElement = new SoapElementAttribute(elementName.DecodedName);
  855. }
  856. else
  857. {
  858. if (member.XmlAttributes == null)
  859. member.XmlAttributes = new XmlAttributes();
  860. else
  861. {
  862. Type invalidAttributeType = null;
  863. if (member.XmlAttributes.XmlAttribute != null)
  864. invalidAttributeType = typeof(XmlAttributeAttribute);
  865. else if (member.XmlAttributes.XmlAnyAttribute != null && !isWrapped)
  866. invalidAttributeType = typeof(XmlAnyAttributeAttribute);
  867. else if (member.XmlAttributes.XmlChoiceIdentifier != null)
  868. invalidAttributeType = typeof(XmlChoiceIdentifierAttribute);
  869. else if (member.XmlAttributes.XmlIgnore)
  870. invalidAttributeType = typeof(XmlIgnoreAttribute);
  871. else if (member.XmlAttributes.Xmlns)
  872. invalidAttributeType = typeof(XmlNamespaceDeclarationsAttribute);
  873. else if (member.XmlAttributes.XmlText != null)
  874. invalidAttributeType = typeof(XmlTextAttribute);
  875. else if (member.XmlAttributes.XmlEnum != null)
  876. invalidAttributeType = typeof(XmlEnumAttribute);
  877. if (invalidAttributeType != null)
  878. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(isWrapped ? SR.SFxInvalidXmlAttributeInWrapped : SR.SFxInvalidXmlAttributeInBare, invalidAttributeType, elementName.DecodedName)));
  879. if (member.XmlAttributes.XmlArray != null && isMultiple)
  880. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxXmlArrayNotAllowedForMultiple, elementName.DecodedName, ns)));
  881. }
  882. bool isArray = member.MemberType.IsArray;
  883. if ((isArray && !isMultiple && member.MemberType != typeof(byte[])) ||
  884. (!isArray && typeof(IEnumerable).IsAssignableFrom(member.MemberType) && member.MemberType != typeof(string) && !typeof(XmlNode).IsAssignableFrom(member.MemberType) && !typeof(IXmlSerializable).IsAssignableFrom(member.MemberType)))
  885. {
  886. if (member.XmlAttributes.XmlArray != null)
  887. {
  888. if (member.XmlAttributes.XmlArray.ElementName == String.Empty)
  889. member.XmlAttributes.XmlArray.ElementName = elementName.DecodedName;
  890. if (member.XmlAttributes.XmlArray.Namespace == null)
  891. member.XmlAttributes.XmlArray.Namespace = ns;
  892. }
  893. else if (HasNoXmlParameterAttributes(member.XmlAttributes))
  894. {
  895. member.XmlAttributes.XmlArray = new XmlArrayAttribute();
  896. member.XmlAttributes.XmlArray.ElementName = elementName.DecodedName;
  897. member.XmlAttributes.XmlArray.Namespace = ns;
  898. }
  899. }
  900. else
  901. {
  902. if (member.XmlAttributes.XmlElements == null || member.XmlAttributes.XmlElements.Count == 0)
  903. {
  904. if (HasNoXmlParameterAttributes(member.XmlAttributes))
  905. {
  906. XmlElementAttribute elementAttribute = new XmlElementAttribute();
  907. elementAttribute.ElementName = elementName.DecodedName;
  908. elementAttribute.Namespace = ns;
  909. member.XmlAttributes.XmlElements.Add(elementAttribute);
  910. }
  911. }
  912. else
  913. {
  914. foreach (XmlElementAttribute elementAttribute in member.XmlAttributes.XmlElements)
  915. {
  916. if (elementAttribute.ElementName == String.Empty)
  917. elementAttribute.ElementName = elementName.DecodedName;
  918. if (elementAttribute.Namespace == null)
  919. elementAttribute.Namespace = ns;
  920. }
  921. }
  922. }
  923. }
  924. return member;
  925. }
  926. static bool HasNoXmlParameterAttributes(XmlAttributes xmlAttributes)
  927. {
  928. return xmlAttributes.XmlAnyAttribute == null &&
  929. (xmlAttributes.XmlAnyElements == null || xmlAttributes.XmlAnyElements.Count == 0) &&
  930. xmlAttributes.XmlArray == null &&
  931. xmlAttributes.XmlAttribute == null &&
  932. !xmlAttributes.XmlIgnore &&
  933. xmlAttributes.XmlText == null &&
  934. xmlAttributes.XmlChoiceIdentifier == null &&
  935. (xmlAttributes.XmlElements == null || xmlAttributes.XmlElements.Count == 0) &&
  936. !xmlAttributes.Xmlns;
  937. }
  938. }
  939. }