2
0

AddressHeader.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // System.ServiceModel.AddressHeader.cs
  3. //
  4. // Author: Duncan Mak ([email protected])
  5. //
  6. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Runtime.Serialization;
  29. using System.ServiceModel;
  30. using System.Xml;
  31. namespace System.ServiceModel.Channels
  32. {
  33. public abstract class AddressHeader
  34. {
  35. protected AddressHeader () {}
  36. public static AddressHeader CreateAddressHeader (object value)
  37. {
  38. return new DefaultAddressHeader (value);
  39. }
  40. public static AddressHeader CreateAddressHeader (object value, XmlObjectSerializer formatter)
  41. {
  42. return new DefaultAddressHeader (value, formatter);
  43. }
  44. public static AddressHeader CreateAddressHeader (string name, string ns, object value)
  45. {
  46. return new DefaultAddressHeader (name, ns, value);
  47. }
  48. public static AddressHeader CreateAddressHeader (string name, string ns, object value,
  49. XmlObjectSerializer formatter)
  50. {
  51. if (formatter == null)
  52. throw new ArgumentNullException ("formatter");
  53. return new DefaultAddressHeader (name, ns, value, formatter);
  54. }
  55. public override bool Equals (object obj)
  56. {
  57. AddressHeader o = obj as AddressHeader;
  58. if (o == null)
  59. return false;
  60. return o.Name == this.Name && o.Namespace == this.Namespace;
  61. }
  62. [MonoTODO]
  63. public virtual XmlDictionaryReader GetAddressHeaderReader ()
  64. {
  65. throw new NotImplementedException ();
  66. }
  67. public override int GetHashCode ()
  68. {
  69. return this.Name.GetHashCode () + this.Namespace.GetHashCode ();
  70. }
  71. public T GetValue<T> ()
  72. {
  73. return GetValue<T> (new DataContractSerializer (typeof (T)));
  74. }
  75. public T GetValue<T> (XmlObjectSerializer formatter)
  76. {
  77. throw new NotImplementedException ();
  78. }
  79. protected abstract void OnWriteAddressHeaderContents (XmlDictionaryWriter writer);
  80. protected virtual void OnWriteStartAddressHeader (XmlDictionaryWriter writer)
  81. {
  82. if (Name != null && Namespace != null)
  83. writer.WriteStartElement (Name, Namespace);
  84. }
  85. public MessageHeader ToMessageHeader ()
  86. {
  87. throw new NotImplementedException ();
  88. }
  89. public void WriteAddressHeader (XmlDictionaryWriter writer)
  90. {
  91. if (writer == null)
  92. throw new ArgumentNullException ("writer is null");
  93. this.WriteStartAddressHeader (writer);
  94. this.WriteAddressHeaderContents (writer);
  95. if (Name != null && Namespace != null)
  96. writer.WriteEndElement ();
  97. }
  98. public void WriteAddressHeader (XmlWriter writer)
  99. {
  100. this.WriteAddressHeader (XmlDictionaryWriter.CreateDictionaryWriter (writer));
  101. }
  102. public void WriteAddressHeaderContents (XmlDictionaryWriter writer)
  103. {
  104. this.OnWriteAddressHeaderContents (writer);
  105. }
  106. public void WriteStartAddressHeader (XmlDictionaryWriter writer)
  107. {
  108. this.OnWriteStartAddressHeader (writer);
  109. }
  110. public abstract string Name { get; }
  111. public abstract string Namespace { get; }
  112. internal class DefaultAddressHeader : AddressHeader
  113. {
  114. string name, ns;
  115. XmlObjectSerializer formatter;
  116. object value;
  117. internal DefaultAddressHeader (object value)
  118. : this (null, null, value) {}
  119. internal DefaultAddressHeader (object value, XmlObjectSerializer formatter)
  120. : this (null, null, value, formatter)
  121. {
  122. }
  123. internal DefaultAddressHeader (string name, string ns, object value)
  124. : this (name, ns, value, null) {}
  125. internal DefaultAddressHeader (string name, string ns, object value, XmlObjectSerializer formatter)
  126. {
  127. if (formatter == null) {
  128. if (value == null)
  129. formatter = new NetDataContractSerializer ();
  130. else
  131. formatter = new DataContractSerializer (value.GetType ());
  132. }
  133. this.name = name;
  134. this.ns = ns;
  135. this.formatter = formatter;
  136. this.value = value;
  137. }
  138. public override string Name {
  139. get { return name; }
  140. }
  141. public override string Namespace {
  142. get { return ns; }
  143. }
  144. protected override void OnWriteAddressHeaderContents (XmlDictionaryWriter writer)
  145. {
  146. this.formatter.WriteObject (writer, value);
  147. }
  148. }
  149. }
  150. }