PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.ServiceModel/System.ServiceModel.Channels/MessageProperties.cs

https://bitbucket.org/danipen/mono
C# | 313 lines | 237 code | 49 blank | 27 comment | 24 complexity | 697277416db9d2b1101a3861dfdba633 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // MessageProperties.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <atsushi@ximian.com>
  6. //
  7. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  8. //
  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:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  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.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Collections.ObjectModel;
  32. using System.ServiceModel.Channels;
  33. using System.ServiceModel.Security;
  34. using Pair = System.Collections.Generic.KeyValuePair<string, object>;
  35. namespace System.ServiceModel.Channels
  36. {
  37. public sealed class MessageProperties : IDictionary<string, object>,
  38. ICollection<Pair>, IEnumerable<Pair>, IEnumerable, IDisposable
  39. {
  40. List<Pair> list;
  41. public MessageProperties ()
  42. {
  43. list = new List<Pair> ();
  44. }
  45. public MessageProperties (MessageProperties properties)
  46. {
  47. list = new List<Pair> ();
  48. CopyProperties (properties);
  49. }
  50. public bool AllowOutputBatching {
  51. get {
  52. var obj = this ["AllowOutputBatching"];
  53. return obj != null ? (bool) obj : false;
  54. }
  55. set { this ["AllowOutputBatching"] = value; }
  56. }
  57. public int Count {
  58. get { return list.Count; }
  59. }
  60. public MessageEncoder Encoder {
  61. get { return (MessageEncoder) this ["Encoder"]; }
  62. set { this ["Encoder"] = value; }
  63. }
  64. public bool IsFixedSize {
  65. get { return false; }
  66. }
  67. public bool IsReadOnly {
  68. get { return false; }
  69. }
  70. public ICollection<string> Keys {
  71. get { return new ParameterKeyCollection (list); }
  72. }
  73. public object this [string name] {
  74. get {
  75. for (int i = 0; i < list.Count; i++)
  76. if (list [i].Key == name)
  77. return list [i].Value;
  78. return null;
  79. }
  80. set {
  81. for (int i = 0; i < list.Count; i++)
  82. if (list [i].Key == name) {
  83. list [i] = new Pair (name, value);
  84. return;
  85. }
  86. list.Add (new Pair (name, value));
  87. }
  88. }
  89. #if !NET_2_1
  90. public SecurityMessageProperty Security {
  91. get { return (SecurityMessageProperty) this ["Security"]; }
  92. set { this ["Security"] = value; }
  93. }
  94. #endif
  95. public ICollection<object> Values {
  96. get { return new ParameterValueCollection (list); }
  97. }
  98. public Uri Via {
  99. get { return (Uri) this ["Via"]; }
  100. set { this ["Via"] = value; }
  101. }
  102. public void Add (string name, object property)
  103. {
  104. list.Add (new Pair (name, property));
  105. }
  106. public void Clear ()
  107. {
  108. list.Clear ();
  109. }
  110. public bool ContainsKey (string name)
  111. {
  112. for (int i = 0; i < list.Count; i++)
  113. if (list [i].Key == name)
  114. return true;
  115. return false;
  116. }
  117. public void CopyProperties (MessageProperties properties)
  118. {
  119. list.AddRange (properties.list);
  120. }
  121. public void Dispose ()
  122. {
  123. }
  124. public bool Remove (string name)
  125. {
  126. for (int i = 0; i < list.Count; i++)
  127. if (list [i].Key == name) {
  128. list.RemoveAt (i);
  129. return true;
  130. }
  131. return false;
  132. }
  133. public bool TryGetValue (string name, out object value)
  134. {
  135. for (int i = 0; i < list.Count; i++)
  136. if (list [i].Key == name) {
  137. value = list [i].Value;
  138. return true;
  139. }
  140. value = null;
  141. return false;
  142. }
  143. void ICollection<Pair>.Add (Pair pair)
  144. {
  145. list.Add (pair);
  146. }
  147. bool ICollection<Pair>.Contains (Pair pair)
  148. {
  149. return list.Contains (pair);
  150. }
  151. void ICollection<Pair>.CopyTo (Pair [] array, int index)
  152. {
  153. list.CopyTo (array, index);
  154. }
  155. bool ICollection<Pair>.Remove (Pair pair)
  156. {
  157. return list.Remove (pair);
  158. }
  159. IEnumerator<Pair> IEnumerable<Pair>.GetEnumerator ()
  160. {
  161. return list.GetEnumerator ();
  162. }
  163. IEnumerator IEnumerable.GetEnumerator ()
  164. {
  165. return (IEnumerator) ((IEnumerable<Pair>) this).GetEnumerator ();
  166. }
  167. class ParameterKeyCollection : ICollection<string>
  168. {
  169. List<Pair> source;
  170. public ParameterKeyCollection (List<Pair> source)
  171. {
  172. this.source = source;
  173. }
  174. public int Count {
  175. get { return source.Count; }
  176. }
  177. public bool IsReadOnly {
  178. get { return true; }
  179. }
  180. public void Add (string item)
  181. {
  182. throw new InvalidOperationException ();
  183. }
  184. public void Clear ()
  185. {
  186. throw new InvalidOperationException ();
  187. }
  188. public bool Contains (string item)
  189. {
  190. for (int i = 0; i < source.Count; i++)
  191. if (source [i].Key == item)
  192. return true;
  193. return false;
  194. }
  195. public void CopyTo (string [] array, int index)
  196. {
  197. for (int i = 0; i < source.Count; i++)
  198. array [index + i] = source [i].Key;
  199. }
  200. public IEnumerator<string> GetEnumerator ()
  201. {
  202. foreach (Pair p in source)
  203. yield return p.Key;
  204. }
  205. IEnumerator IEnumerable.GetEnumerator ()
  206. {
  207. foreach (Pair p in source)
  208. yield return p.Key;
  209. }
  210. public bool Remove (string item)
  211. {
  212. throw new InvalidOperationException ();
  213. }
  214. }
  215. class ParameterValueCollection : ICollection<object>
  216. {
  217. List<Pair> source;
  218. public ParameterValueCollection (List<Pair> source)
  219. {
  220. this.source = source;
  221. }
  222. public int Count {
  223. get { return source.Count; }
  224. }
  225. public bool IsReadOnly {
  226. get { return true; }
  227. }
  228. public void Add (object item)
  229. {
  230. throw new InvalidOperationException ();
  231. }
  232. public void Clear ()
  233. {
  234. throw new InvalidOperationException ();
  235. }
  236. public bool Contains (object item)
  237. {
  238. for (int i = 0; i < source.Count; i++)
  239. if (source [i].Value == item)
  240. return true;
  241. return false;
  242. }
  243. public void CopyTo (object [] array, int index)
  244. {
  245. for (int i = 0; i < source.Count; i++)
  246. array [index + i] = source [i].Value;
  247. }
  248. public IEnumerator<object> GetEnumerator ()
  249. {
  250. foreach (Pair p in source)
  251. yield return p.Value;
  252. }
  253. IEnumerator IEnumerable.GetEnumerator ()
  254. {
  255. foreach (Pair p in source)
  256. yield return p.Key;
  257. }
  258. public bool Remove (object item)
  259. {
  260. throw new InvalidOperationException ();
  261. }
  262. }
  263. }
  264. }