ReadOnlyPropertiesDictionary.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. using System.Reflection;
  22. #if !NETCF
  23. using System.Runtime.Serialization;
  24. using System.Xml;
  25. #endif
  26. namespace log4net.Util
  27. {
  28. /// <summary>
  29. /// String keyed object map that is read only.
  30. /// </summary>
  31. /// <remarks>
  32. /// <para>
  33. /// This collection is readonly and cannot be modified.
  34. /// </para>
  35. /// <para>
  36. /// While this collection is serializable only member
  37. /// objects that are serializable will
  38. /// be serialized along with this collection.
  39. /// </para>
  40. /// </remarks>
  41. /// <author>Nicko Cadell</author>
  42. /// <author>Gert Driesen</author>
  43. #if NETCF
  44. public class ReadOnlyPropertiesDictionary : IDictionary
  45. #else
  46. [Serializable] public class ReadOnlyPropertiesDictionary : ISerializable, IDictionary
  47. #endif
  48. {
  49. #region Private Instance Fields
  50. /// <summary>
  51. /// The Hashtable used to store the properties data
  52. /// </summary>
  53. private readonly Hashtable m_hashtable = new Hashtable();
  54. #endregion Private Instance Fields
  55. #region Public Instance Constructors
  56. /// <summary>
  57. /// Constructor
  58. /// </summary>
  59. /// <remarks>
  60. /// <para>
  61. /// Initializes a new instance of the <see cref="ReadOnlyPropertiesDictionary" /> class.
  62. /// </para>
  63. /// </remarks>
  64. public ReadOnlyPropertiesDictionary()
  65. {
  66. }
  67. /// <summary>
  68. /// Copy Constructor
  69. /// </summary>
  70. /// <param name="propertiesDictionary">properties to copy</param>
  71. /// <remarks>
  72. /// <para>
  73. /// Initializes a new instance of the <see cref="ReadOnlyPropertiesDictionary" /> class.
  74. /// </para>
  75. /// </remarks>
  76. public ReadOnlyPropertiesDictionary(ReadOnlyPropertiesDictionary propertiesDictionary)
  77. {
  78. foreach(DictionaryEntry entry in propertiesDictionary)
  79. {
  80. InnerHashtable.Add(entry.Key, entry.Value);
  81. }
  82. }
  83. #endregion Public Instance Constructors
  84. #region Private Instance Constructors
  85. #if !(NETCF || NETSTANDARD1_3)
  86. /// <summary>
  87. /// Deserialization constructor
  88. /// </summary>
  89. /// <param name="info">The <see cref="SerializationInfo" /> that holds the serialized object data.</param>
  90. /// <param name="context">The <see cref="StreamingContext" /> that contains contextual information about the source or destination.</param>
  91. /// <remarks>
  92. /// <para>
  93. /// Initializes a new instance of the <see cref="ReadOnlyPropertiesDictionary" /> class
  94. /// with serialized data.
  95. /// </para>
  96. /// </remarks>
  97. protected ReadOnlyPropertiesDictionary(SerializationInfo info, StreamingContext context)
  98. {
  99. foreach(SerializationEntry entry in info)
  100. {
  101. // The keys are stored as Xml encoded names
  102. InnerHashtable[XmlConvert.DecodeName(entry.Name)] = entry.Value;
  103. }
  104. }
  105. #endif
  106. #endregion Protected Instance Constructors
  107. #region Public Instance Properties
  108. /// <summary>
  109. /// Gets the key names.
  110. /// </summary>
  111. /// <returns>An array of all the keys.</returns>
  112. /// <remarks>
  113. /// <para>
  114. /// Gets the key names.
  115. /// </para>
  116. /// </remarks>
  117. public string[] GetKeys()
  118. {
  119. string[] keys = new String[InnerHashtable.Count];
  120. InnerHashtable.Keys.CopyTo(keys, 0);
  121. return keys;
  122. }
  123. /// <summary>
  124. /// Gets or sets the value of the property with the specified key.
  125. /// </summary>
  126. /// <value>
  127. /// The value of the property with the specified key.
  128. /// </value>
  129. /// <param name="key">The key of the property to get or set.</param>
  130. /// <remarks>
  131. /// <para>
  132. /// The property value will only be serialized if it is serializable.
  133. /// If it cannot be serialized it will be silently ignored if
  134. /// a serialization operation is performed.
  135. /// </para>
  136. /// </remarks>
  137. public virtual object this[string key]
  138. {
  139. get { return InnerHashtable[key]; }
  140. set { throw new NotSupportedException("This is a Read Only Dictionary and can not be modified"); }
  141. }
  142. #endregion Public Instance Properties
  143. #region Public Instance Methods
  144. /// <summary>
  145. /// Test if the dictionary contains a specified key
  146. /// </summary>
  147. /// <param name="key">the key to look for</param>
  148. /// <returns>true if the dictionary contains the specified key</returns>
  149. /// <remarks>
  150. /// <para>
  151. /// Test if the dictionary contains a specified key
  152. /// </para>
  153. /// </remarks>
  154. public bool Contains(string key)
  155. {
  156. return InnerHashtable.Contains(key);
  157. }
  158. #endregion
  159. /// <summary>
  160. /// The hashtable used to store the properties
  161. /// </summary>
  162. /// <value>
  163. /// The internal collection used to store the properties
  164. /// </value>
  165. /// <remarks>
  166. /// <para>
  167. /// The hashtable used to store the properties
  168. /// </para>
  169. /// </remarks>
  170. protected Hashtable InnerHashtable
  171. {
  172. get { return m_hashtable; }
  173. }
  174. #region Implementation of ISerializable
  175. #if !NETCF
  176. /// <summary>
  177. /// Serializes this object into the <see cref="SerializationInfo" /> provided.
  178. /// </summary>
  179. /// <param name="info">The <see cref="SerializationInfo" /> to populate with data.</param>
  180. /// <param name="context">The destination for this serialization.</param>
  181. /// <remarks>
  182. /// <para>
  183. /// Serializes this object into the <see cref="SerializationInfo" /> provided.
  184. /// </para>
  185. /// </remarks>
  186. #if NET_4_0 || MONO_4_0 || NETSTANDARD1_3
  187. [System.Security.SecurityCritical]
  188. #else
  189. [System.Security.Permissions.SecurityPermissionAttribute(System.Security.Permissions.SecurityAction.Demand, SerializationFormatter=true)]
  190. #endif
  191. public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
  192. {
  193. foreach(DictionaryEntry entry in InnerHashtable.Clone() as IDictionary)
  194. {
  195. string entryKey = entry.Key as string;
  196. object entryValue = entry.Value;
  197. // If value is serializable then we add it to the list
  198. #if NETSTANDARD1_3
  199. bool isSerializable = entryValue.GetType().GetTypeInfo().IsSerializable;
  200. #else
  201. bool isSerializable = entryValue.GetType().IsSerializable;
  202. #endif
  203. if (entryKey != null && entryValue != null && isSerializable)
  204. {
  205. // Store the keys as an Xml encoded local name as it may contain colons (':')
  206. // which are NOT escaped by the Xml Serialization framework.
  207. // This must be a bug in the serialization framework as we cannot be expected
  208. // to know the implementation details of all the possible transport layers.
  209. info.AddValue(XmlConvert.EncodeLocalName(entryKey), entryValue);
  210. }
  211. }
  212. }
  213. #endif
  214. #endregion Implementation of ISerializable
  215. #region Implementation of IDictionary
  216. /// <summary>
  217. /// See <see cref="IDictionary.GetEnumerator"/>
  218. /// </summary>
  219. IDictionaryEnumerator IDictionary.GetEnumerator()
  220. {
  221. return InnerHashtable.GetEnumerator();
  222. }
  223. /// <summary>
  224. /// See <see cref="IDictionary.Remove"/>
  225. /// </summary>
  226. /// <param name="key"></param>
  227. void IDictionary.Remove(object key)
  228. {
  229. throw new NotSupportedException("This is a Read Only Dictionary and can not be modified");
  230. }
  231. /// <summary>
  232. /// See <see cref="IDictionary.Contains"/>
  233. /// </summary>
  234. /// <param name="key"></param>
  235. /// <returns></returns>
  236. bool IDictionary.Contains(object key)
  237. {
  238. return InnerHashtable.Contains(key);
  239. }
  240. /// <summary>
  241. /// Remove all properties from the properties collection
  242. /// </summary>
  243. public virtual void Clear()
  244. {
  245. throw new NotSupportedException("This is a Read Only Dictionary and can not be modified");
  246. }
  247. /// <summary>
  248. /// See <see cref="IDictionary.Add"/>
  249. /// </summary>
  250. /// <param name="key"></param>
  251. /// <param name="value"></param>
  252. void IDictionary.Add(object key, object value)
  253. {
  254. throw new NotSupportedException("This is a Read Only Dictionary and can not be modified");
  255. }
  256. /// <summary>
  257. /// See <see cref="IDictionary.IsReadOnly"/>
  258. /// </summary>
  259. bool IDictionary.IsReadOnly
  260. {
  261. get { return true; }
  262. }
  263. /// <summary>
  264. /// See <see cref="IDictionary.this[object]"/>
  265. /// </summary>
  266. object IDictionary.this[object key]
  267. {
  268. get
  269. {
  270. if (!(key is string)) throw new ArgumentException("key must be a string");
  271. return InnerHashtable[key];
  272. }
  273. set
  274. {
  275. throw new NotSupportedException("This is a Read Only Dictionary and can not be modified");
  276. }
  277. }
  278. /// <summary>
  279. /// See <see cref="IDictionary.Values"/>
  280. /// </summary>
  281. ICollection IDictionary.Values
  282. {
  283. get { return InnerHashtable.Values; }
  284. }
  285. /// <summary>
  286. /// See <see cref="IDictionary.Keys"/>
  287. /// </summary>
  288. ICollection IDictionary.Keys
  289. {
  290. get { return InnerHashtable.Keys; }
  291. }
  292. /// <summary>
  293. /// See <see cref="IDictionary.IsFixedSize"/>
  294. /// </summary>
  295. bool IDictionary.IsFixedSize
  296. {
  297. get { return InnerHashtable.IsFixedSize; }
  298. }
  299. #endregion
  300. #region Implementation of ICollection
  301. /// <summary>
  302. /// See <see cref="ICollection.CopyTo"/>
  303. /// </summary>
  304. /// <param name="array"></param>
  305. /// <param name="index"></param>
  306. void ICollection.CopyTo(Array array, int index)
  307. {
  308. InnerHashtable.CopyTo(array, index);
  309. }
  310. /// <summary>
  311. /// See <see cref="ICollection.IsSynchronized"/>
  312. /// </summary>
  313. bool ICollection.IsSynchronized
  314. {
  315. get { return InnerHashtable.IsSynchronized; }
  316. }
  317. /// <summary>
  318. /// The number of properties in this collection
  319. /// </summary>
  320. public int Count
  321. {
  322. get { return InnerHashtable.Count; }
  323. }
  324. /// <summary>
  325. /// See <see cref="ICollection.SyncRoot"/>
  326. /// </summary>
  327. object ICollection.SyncRoot
  328. {
  329. get { return InnerHashtable.SyncRoot; }
  330. }
  331. #endregion
  332. #region Implementation of IEnumerable
  333. /// <summary>
  334. /// See <see cref="IEnumerable.GetEnumerator"/>
  335. /// </summary>
  336. IEnumerator IEnumerable.GetEnumerator()
  337. {
  338. return ((IEnumerable)InnerHashtable).GetEnumerator();
  339. }
  340. #endregion
  341. }
  342. }