XmlSerializerObjectSerializer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Dispatcher
  5. {
  6. using System;
  7. using System.Xml;
  8. using System.ServiceModel;
  9. using System.Xml.Serialization;
  10. using System.Collections.Generic;
  11. using System.Runtime.Serialization;
  12. using System.ServiceModel.Description;
  13. internal class XmlSerializerObjectSerializer : XmlObjectSerializer
  14. {
  15. XmlSerializer serializer;
  16. Type rootType;
  17. string rootName;
  18. string rootNamespace;
  19. bool isSerializerSetExplicit = false;
  20. internal XmlSerializerObjectSerializer(Type type)
  21. {
  22. Initialize(type, null /*rootName*/, null /*rootNamespace*/, null /*xmlSerializer*/);
  23. }
  24. internal XmlSerializerObjectSerializer(Type type, XmlQualifiedName qualifiedName, XmlSerializer xmlSerializer)
  25. {
  26. if (qualifiedName == null)
  27. {
  28. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("qualifiedName");
  29. }
  30. Initialize(type, qualifiedName.Name, qualifiedName.Namespace, xmlSerializer);
  31. }
  32. void Initialize(Type type, string rootName, string rootNamespace, XmlSerializer xmlSerializer)
  33. {
  34. if (type == null)
  35. {
  36. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("type");
  37. }
  38. this.rootType = type;
  39. this.rootName = rootName;
  40. this.rootNamespace = rootNamespace == null ? string.Empty : rootNamespace;
  41. this.serializer = xmlSerializer;
  42. if (this.serializer == null)
  43. {
  44. if (this.rootName == null)
  45. this.serializer = new XmlSerializer(type);
  46. else
  47. {
  48. XmlRootAttribute xmlRoot = new XmlRootAttribute();
  49. xmlRoot.ElementName = this.rootName;
  50. xmlRoot.Namespace = this.rootNamespace;
  51. this.serializer = new XmlSerializer(type, xmlRoot);
  52. }
  53. }
  54. else
  55. isSerializerSetExplicit = true;
  56. //try to get rootName and rootNamespace from type since root name not set explicitly
  57. if (this.rootName == null)
  58. {
  59. XmlTypeMapping mapping = new XmlReflectionImporter().ImportTypeMapping(this.rootType);
  60. this.rootName = mapping.ElementName;
  61. this.rootNamespace = mapping.Namespace;
  62. }
  63. }
  64. public override void WriteObject(XmlDictionaryWriter writer, object graph)
  65. {
  66. if (this.isSerializerSetExplicit)
  67. this.serializer.Serialize(writer, new object[] { graph });
  68. else
  69. this.serializer.Serialize(writer, graph);
  70. }
  71. public override void WriteStartObject(XmlDictionaryWriter writer, object graph)
  72. {
  73. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException());
  74. }
  75. public override void WriteObjectContent(XmlDictionaryWriter writer, object graph)
  76. {
  77. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException());
  78. }
  79. public override void WriteEndObject(XmlDictionaryWriter writer)
  80. {
  81. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException());
  82. }
  83. public override object ReadObject(XmlDictionaryReader reader, bool verifyObjectName)
  84. {
  85. if (this.isSerializerSetExplicit)
  86. {
  87. object[] deserializedObjects = (object[])this.serializer.Deserialize(reader);
  88. if (deserializedObjects != null && deserializedObjects.Length > 0)
  89. return deserializedObjects[0];
  90. else
  91. return null;
  92. }
  93. else
  94. return this.serializer.Deserialize(reader);
  95. }
  96. public override bool IsStartObject(XmlDictionaryReader reader)
  97. {
  98. if (reader == null)
  99. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("reader"));
  100. reader.MoveToElement();
  101. if (this.rootName != null)
  102. {
  103. return reader.IsStartElement(this.rootName, this.rootNamespace);
  104. }
  105. else
  106. {
  107. return reader.IsStartElement();
  108. }
  109. }
  110. }
  111. }