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