2 // MessageProperties.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Collections.ObjectModel;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Security;
35 using Pair = System.Collections.Generic.KeyValuePair<string, object>;
37 namespace System.ServiceModel.Channels
39 public sealed class MessageProperties : IDictionary<string, object>,
40 ICollection<Pair>, IEnumerable<Pair>, IEnumerable, IDisposable
44 public MessageProperties ()
46 list = new List<Pair> ();
49 public MessageProperties (MessageProperties properties)
51 list = new List<Pair> ();
52 CopyProperties (properties);
55 public bool AllowOutputBatching {
57 var obj = this ["AllowOutputBatching"];
58 return obj != null ? (bool) obj : false;
60 set { this ["AllowOutputBatching"] = value; }
64 get { return list.Count; }
67 public MessageEncoder Encoder {
68 get { return (MessageEncoder) this ["Encoder"]; }
69 set { this ["Encoder"] = value; }
72 public bool IsFixedSize {
76 public bool IsReadOnly {
80 public ICollection<string> Keys {
81 get { return new ParameterKeyCollection (list); }
84 public object this [string name] {
86 for (int i = 0; i < list.Count; i++)
87 if (list [i].Key == name)
88 return list [i].Value;
92 for (int i = 0; i < list.Count; i++)
93 if (list [i].Key == name) {
94 list [i] = new Pair (name, value);
97 list.Add (new Pair (name, value));
102 public SecurityMessageProperty Security {
103 get { return (SecurityMessageProperty) this ["Security"]; }
104 set { this ["Security"] = value; }
108 public ICollection<object> Values {
109 get { return new ParameterValueCollection (list); }
113 get { return (Uri) this ["Via"]; }
114 set { this ["Via"] = value; }
117 public void Add (string name, object property)
119 list.Add (new Pair (name, property));
127 public bool ContainsKey (string name)
129 for (int i = 0; i < list.Count; i++)
130 if (list [i].Key == name)
135 public void CopyProperties (MessageProperties properties)
137 list.AddRange (properties.list);
140 public void Dispose ()
144 public bool Remove (string name)
146 for (int i = 0; i < list.Count; i++)
147 if (list [i].Key == name) {
154 public bool TryGetValue (string name, out object value)
156 for (int i = 0; i < list.Count; i++)
157 if (list [i].Key == name) {
158 value = list [i].Value;
165 void ICollection<Pair>.Add (Pair pair)
170 bool ICollection<Pair>.Contains (Pair pair)
172 return list.Contains (pair);
175 void ICollection<Pair>.CopyTo (Pair [] array, int index)
177 list.CopyTo (array, index);
180 bool ICollection<Pair>.Remove (Pair pair)
182 return list.Remove (pair);
185 IEnumerator<Pair> IEnumerable<Pair>.GetEnumerator ()
187 return list.GetEnumerator ();
190 IEnumerator IEnumerable.GetEnumerator ()
192 return (IEnumerator) ((IEnumerable<Pair>) this).GetEnumerator ();
195 class ParameterKeyCollection : ICollection<string>
199 public ParameterKeyCollection (List<Pair> source)
201 this.source = source;
205 get { return source.Count; }
208 public bool IsReadOnly {
212 public void Add (string item)
214 throw new InvalidOperationException ();
219 throw new InvalidOperationException ();
222 public bool Contains (string item)
224 for (int i = 0; i < source.Count; i++)
225 if (source [i].Key == item)
230 public void CopyTo (string [] array, int index)
232 for (int i = 0; i < source.Count; i++)
233 array [index + i] = source [i].Key;
236 public IEnumerator<string> GetEnumerator ()
238 foreach (Pair p in source)
242 IEnumerator IEnumerable.GetEnumerator ()
244 foreach (Pair p in source)
248 public bool Remove (string item)
250 throw new InvalidOperationException ();
254 class ParameterValueCollection : ICollection<object>
258 public ParameterValueCollection (List<Pair> source)
260 this.source = source;
264 get { return source.Count; }
267 public bool IsReadOnly {
271 public void Add (object item)
273 throw new InvalidOperationException ();
278 throw new InvalidOperationException ();
281 public bool Contains (object item)
283 for (int i = 0; i < source.Count; i++)
284 if (source [i].Value == item)
289 public void CopyTo (object [] array, int index)
291 for (int i = 0; i < source.Count; i++)
292 array [index + i] = source [i].Value;
295 public IEnumerator<object> GetEnumerator ()
297 foreach (Pair p in source)
298 yield return p.Value;
301 IEnumerator IEnumerable.GetEnumerator ()
303 foreach (Pair p in source)
307 public bool Remove (object item)
309 throw new InvalidOperationException ();