2 // System.ServiceModel.AddressHeader.cs
4 // Author: Duncan Mak (duncan@novell.com)
6 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
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.
29 using System.Runtime.Serialization;
30 using System.ServiceModel;
33 namespace System.ServiceModel.Channels
35 public abstract class AddressHeader
37 protected AddressHeader () {}
39 public static AddressHeader CreateAddressHeader (object value)
41 return new DefaultAddressHeader (value);
44 public static AddressHeader CreateAddressHeader (object value, XmlObjectSerializer formatter)
46 return new DefaultAddressHeader (value, formatter);
49 public static AddressHeader CreateAddressHeader (string name, string ns, object value)
51 return new DefaultAddressHeader (name, ns, value);
54 public static AddressHeader CreateAddressHeader (string name, string ns, object value,
55 XmlObjectSerializer formatter)
57 if (formatter == null)
58 throw new ArgumentNullException ("formatter");
59 return new DefaultAddressHeader (name, ns, value, formatter);
62 public override bool Equals (object obj)
64 AddressHeader o = obj as AddressHeader;
69 return o.Name == this.Name && o.Namespace == this.Namespace;
73 public virtual XmlDictionaryReader GetAddressHeaderReader ()
75 throw new NotImplementedException ();
78 public override int GetHashCode ()
80 return this.Name.GetHashCode () + this.Namespace.GetHashCode ();
83 public T GetValue<T> ()
85 return GetValue<T> (new DataContractSerializer (typeof (T)));
88 public T GetValue<T> (XmlObjectSerializer formatter)
90 throw new NotImplementedException ();
93 protected abstract void OnWriteAddressHeaderContents (XmlDictionaryWriter writer);
94 protected virtual void OnWriteStartAddressHeader (XmlDictionaryWriter writer)
96 if (Name != null && Namespace != null)
97 writer.WriteStartElement (Name, Namespace);
100 public MessageHeader ToMessageHeader ()
102 throw new NotImplementedException ();
105 public void WriteAddressHeader (XmlDictionaryWriter writer)
108 throw new ArgumentNullException ("writer is null");
110 this.WriteStartAddressHeader (writer);
111 this.WriteAddressHeaderContents (writer);
112 if (Name != null && Namespace != null)
113 writer.WriteEndElement ();
116 public void WriteAddressHeader (XmlWriter writer)
118 this.WriteAddressHeader (XmlDictionaryWriter.CreateDictionaryWriter (writer));
121 public void WriteAddressHeaderContents (XmlDictionaryWriter writer)
123 this.OnWriteAddressHeaderContents (writer);
126 public void WriteStartAddressHeader (XmlDictionaryWriter writer)
128 this.OnWriteStartAddressHeader (writer);
131 public abstract string Name { get; }
132 public abstract string Namespace { get; }
134 internal class DefaultAddressHeader : AddressHeader
137 XmlObjectSerializer formatter;
140 internal DefaultAddressHeader (object value)
141 : this (null, null, value) {}
144 internal DefaultAddressHeader (object value, XmlObjectSerializer formatter)
145 : this (null, null, value, formatter)
149 internal DefaultAddressHeader (string name, string ns, object value)
150 : this (name, ns, value, null) {}
152 internal DefaultAddressHeader (string name, string ns, object value, XmlObjectSerializer formatter)
154 if (formatter == null) {
156 formatter = new NetDataContractSerializer ();
158 formatter = new DataContractSerializer (value.GetType ());
162 this.formatter = formatter;
166 public override string Name {
170 public override string Namespace {
174 protected override void OnWriteAddressHeaderContents (XmlDictionaryWriter writer)
176 this.formatter.WriteObject (writer, value);