PropertiesDictionary.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #region Apache License
  2. //
  3. // Licensed to the Apache Software Foundation (ASF) under one or more
  4. // contributor license agreements. See the NOTICE file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // The ASF licenses this file to you under the Apache License, Version 2.0
  7. // (the "License"); you may not use this file except in compliance with
  8. // the License. You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. #endregion
  19. using System;
  20. using System.Collections;
  21. #if !NETCF
  22. using System.Runtime.Serialization;
  23. using System.Xml;
  24. #endif
  25. namespace log4net.Util
  26. {
  27. /// <summary>
  28. /// String keyed object map.
  29. /// </summary>
  30. /// <remarks>
  31. /// <para>
  32. /// While this collection is serializable only member
  33. /// objects that are serializable will
  34. /// be serialized along with this collection.
  35. /// </para>
  36. /// </remarks>
  37. /// <author>Nicko Cadell</author>
  38. /// <author>Gert Driesen</author>
  39. #if NETCF
  40. public sealed class PropertiesDictionary : ReadOnlyPropertiesDictionary, IDictionary
  41. #else
  42. [Serializable] public sealed class PropertiesDictionary : ReadOnlyPropertiesDictionary, ISerializable, IDictionary
  43. #endif
  44. {
  45. #region Public Instance Constructors
  46. /// <summary>
  47. /// Constructor
  48. /// </summary>
  49. /// <remarks>
  50. /// <para>
  51. /// Initializes a new instance of the <see cref="PropertiesDictionary" /> class.
  52. /// </para>
  53. /// </remarks>
  54. public PropertiesDictionary()
  55. {
  56. }
  57. /// <summary>
  58. /// Constructor
  59. /// </summary>
  60. /// <param name="propertiesDictionary">properties to copy</param>
  61. /// <remarks>
  62. /// <para>
  63. /// Initializes a new instance of the <see cref="PropertiesDictionary" /> class.
  64. /// </para>
  65. /// </remarks>
  66. public PropertiesDictionary(ReadOnlyPropertiesDictionary propertiesDictionary) : base(propertiesDictionary)
  67. {
  68. }
  69. #endregion Public Instance Constructors
  70. #region Private Instance Constructors
  71. #if !(NETCF || NETSTANDARD1_3)
  72. /// <summary>
  73. /// Initializes a new instance of the <see cref="PropertiesDictionary" /> class
  74. /// with serialized data.
  75. /// </summary>
  76. /// <param name="info">The <see cref="SerializationInfo" /> that holds the serialized object data.</param>
  77. /// <param name="context">The <see cref="StreamingContext" /> that contains contextual information about the source or destination.</param>
  78. /// <remarks>
  79. /// <para>
  80. /// Because this class is sealed the serialization constructor is private.
  81. /// </para>
  82. /// </remarks>
  83. private PropertiesDictionary(SerializationInfo info, StreamingContext context) : base(info, context)
  84. {
  85. }
  86. #endif
  87. #endregion Protected Instance Constructors
  88. #region Public Instance Properties
  89. /// <summary>
  90. /// Gets or sets the value of the property with the specified key.
  91. /// </summary>
  92. /// <value>
  93. /// The value of the property with the specified key.
  94. /// </value>
  95. /// <param name="key">The key of the property to get or set.</param>
  96. /// <remarks>
  97. /// <para>
  98. /// The property value will only be serialized if it is serializable.
  99. /// If it cannot be serialized it will be silently ignored if
  100. /// a serialization operation is performed.
  101. /// </para>
  102. /// </remarks>
  103. override public object this[string key]
  104. {
  105. get { return InnerHashtable[key]; }
  106. set { InnerHashtable[key] = value; }
  107. }
  108. #endregion Public Instance Properties
  109. #region Public Instance Methods
  110. /// <summary>
  111. /// Remove the entry with the specified key from this dictionary
  112. /// </summary>
  113. /// <param name="key">the key for the entry to remove</param>
  114. /// <remarks>
  115. /// <para>
  116. /// Remove the entry with the specified key from this dictionary
  117. /// </para>
  118. /// </remarks>
  119. public void Remove(string key)
  120. {
  121. InnerHashtable.Remove(key);
  122. }
  123. #endregion Public Instance Methods
  124. #region Implementation of IDictionary
  125. /// <summary>
  126. /// See <see cref="IDictionary.GetEnumerator"/>
  127. /// </summary>
  128. /// <returns>an enumerator</returns>
  129. /// <remarks>
  130. /// <para>
  131. /// Returns a <see cref="IDictionaryEnumerator"/> over the contest of this collection.
  132. /// </para>
  133. /// </remarks>
  134. IDictionaryEnumerator IDictionary.GetEnumerator()
  135. {
  136. return InnerHashtable.GetEnumerator();
  137. }
  138. /// <summary>
  139. /// See <see cref="IDictionary.Remove"/>
  140. /// </summary>
  141. /// <param name="key">the key to remove</param>
  142. /// <remarks>
  143. /// <para>
  144. /// Remove the entry with the specified key from this dictionary
  145. /// </para>
  146. /// </remarks>
  147. void IDictionary.Remove(object key)
  148. {
  149. InnerHashtable.Remove(key);
  150. }
  151. /// <summary>
  152. /// See <see cref="IDictionary.Contains"/>
  153. /// </summary>
  154. /// <param name="key">the key to lookup in the collection</param>
  155. /// <returns><c>true</c> if the collection contains the specified key</returns>
  156. /// <remarks>
  157. /// <para>
  158. /// Test if this collection contains a specified key.
  159. /// </para>
  160. /// </remarks>
  161. bool IDictionary.Contains(object key)
  162. {
  163. return InnerHashtable.Contains(key);
  164. }
  165. /// <summary>
  166. /// Remove all properties from the properties collection
  167. /// </summary>
  168. /// <remarks>
  169. /// <para>
  170. /// Remove all properties from the properties collection
  171. /// </para>
  172. /// </remarks>
  173. public override void Clear()
  174. {
  175. InnerHashtable.Clear();
  176. }
  177. /// <summary>
  178. /// See <see cref="IDictionary.Add"/>
  179. /// </summary>
  180. /// <param name="key">the key</param>
  181. /// <param name="value">the value to store for the key</param>
  182. /// <remarks>
  183. /// <para>
  184. /// Store a value for the specified <see cref="String"/> <paramref name="key"/>.
  185. /// </para>
  186. /// </remarks>
  187. /// <exception cref="ArgumentException">Thrown if the <paramref name="key"/> is not a string</exception>
  188. void IDictionary.Add(object key, object value)
  189. {
  190. if (!(key is string))
  191. {
  192. throw new ArgumentException("key must be a string", "key");
  193. }
  194. InnerHashtable.Add(key, value);
  195. }
  196. /// <summary>
  197. /// See <see cref="IDictionary.IsReadOnly"/>
  198. /// </summary>
  199. /// <value>
  200. /// <c>false</c>
  201. /// </value>
  202. /// <remarks>
  203. /// <para>
  204. /// This collection is modifiable. This property always
  205. /// returns <c>false</c>.
  206. /// </para>
  207. /// </remarks>
  208. bool IDictionary.IsReadOnly
  209. {
  210. get { return false; }
  211. }
  212. /// <summary>
  213. /// See <see cref="IDictionary.this"/>
  214. /// </summary>
  215. /// <value>
  216. /// The value for the key specified.
  217. /// </value>
  218. /// <remarks>
  219. /// <para>
  220. /// Get or set a value for the specified <see cref="String"/> <paramref name="key"/>.
  221. /// </para>
  222. /// </remarks>
  223. /// <exception cref="ArgumentException">Thrown if the <paramref name="key"/> is not a string</exception>
  224. object IDictionary.this[object key]
  225. {
  226. get
  227. {
  228. if (!(key is string))
  229. {
  230. throw new ArgumentException("key must be a string", "key");
  231. }
  232. return InnerHashtable[key];
  233. }
  234. set
  235. {
  236. if (!(key is string))
  237. {
  238. throw new ArgumentException("key must be a string", "key");
  239. }
  240. InnerHashtable[key] = value;
  241. }
  242. }
  243. /// <summary>
  244. /// See <see cref="IDictionary.Values"/>
  245. /// </summary>
  246. ICollection IDictionary.Values
  247. {
  248. get { return InnerHashtable.Values; }
  249. }
  250. /// <summary>
  251. /// See <see cref="IDictionary.Keys"/>
  252. /// </summary>
  253. ICollection IDictionary.Keys
  254. {
  255. get { return InnerHashtable.Keys; }
  256. }
  257. /// <summary>
  258. /// See <see cref="IDictionary.IsFixedSize"/>
  259. /// </summary>
  260. bool IDictionary.IsFixedSize
  261. {
  262. get { return false; }
  263. }
  264. #endregion
  265. #region Implementation of ICollection
  266. /// <summary>
  267. /// See <see cref="ICollection.CopyTo"/>
  268. /// </summary>
  269. /// <param name="array"></param>
  270. /// <param name="index"></param>
  271. void ICollection.CopyTo(Array array, int index)
  272. {
  273. InnerHashtable.CopyTo(array, index);
  274. }
  275. /// <summary>
  276. /// See <see cref="ICollection.IsSynchronized"/>
  277. /// </summary>
  278. bool ICollection.IsSynchronized
  279. {
  280. get { return InnerHashtable.IsSynchronized; }
  281. }
  282. /// <summary>
  283. /// See <see cref="ICollection.SyncRoot"/>
  284. /// </summary>
  285. object ICollection.SyncRoot
  286. {
  287. get { return InnerHashtable.SyncRoot; }
  288. }
  289. #endregion
  290. #region Implementation of IEnumerable
  291. /// <summary>
  292. /// See <see cref="IEnumerable.GetEnumerator"/>
  293. /// </summary>
  294. IEnumerator IEnumerable.GetEnumerator()
  295. {
  296. return ((IEnumerable)InnerHashtable).GetEnumerator();
  297. }
  298. #endregion
  299. }
  300. }