mscorlib(4.0.0.0) API with additions
StringDictionary.cs
4 
6 {
9  [DesignerSerializer("System.Diagnostics.Design.StringDictionaryCodeDomSerializer, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.Serialization.CodeDomSerializer, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
11  {
12  private class GenericAdapter : IDictionary<string, string>, ICollection<KeyValuePair<string, string>>, IEnumerable<KeyValuePair<string, string>>, IEnumerable
13  {
14  internal enum KeyOrValue
15  {
16  Key,
17  Value
18  }
19 
20  private class ICollectionToGenericCollectionAdapter : ICollection<string>, IEnumerable<string>, IEnumerable
21  {
22  private StringDictionary _internal;
23 
24  private KeyOrValue _keyOrValue;
25 
26  public int Count => _internal.Count;
27 
28  public bool IsReadOnly => true;
29 
30  public ICollectionToGenericCollectionAdapter(StringDictionary source, KeyOrValue keyOrValue)
31  {
32  if (source == null)
33  {
34  throw new ArgumentNullException("source");
35  }
36  _internal = source;
37  _keyOrValue = keyOrValue;
38  }
39 
40  public void Add(string item)
41  {
42  ThrowNotSupportedException();
43  }
44 
45  public void Clear()
46  {
47  ThrowNotSupportedException();
48  }
49 
50  public void ThrowNotSupportedException()
51  {
52  if (_keyOrValue == KeyOrValue.Key)
53  {
54  throw new NotSupportedException(SR.GetString("NotSupported_KeyCollectionSet"));
55  }
56  throw new NotSupportedException(SR.GetString("NotSupported_ValueCollectionSet"));
57  }
58 
59  public bool Contains(string item)
60  {
61  if (_keyOrValue == KeyOrValue.Key)
62  {
63  return _internal.ContainsKey(item);
64  }
65  return _internal.ContainsValue(item);
66  }
67 
68  public void CopyTo(string[] array, int arrayIndex)
69  {
70  ICollection underlyingCollection = GetUnderlyingCollection();
71  underlyingCollection.CopyTo(array, arrayIndex);
72  }
73 
74  public bool Remove(string item)
75  {
76  ThrowNotSupportedException();
77  return false;
78  }
79 
80  private ICollection GetUnderlyingCollection()
81  {
82  if (_keyOrValue == KeyOrValue.Key)
83  {
84  return _internal.Keys;
85  }
86  return _internal.Values;
87  }
88 
90  {
91  ICollection underlyingCollection = GetUnderlyingCollection();
92  foreach (string item in underlyingCollection)
93  {
94  yield return item;
95  }
96  }
97 
99  {
100  return GetUnderlyingCollection().GetEnumerator();
101  }
102  }
103 
104  private StringDictionary m_stringDictionary;
105 
106  private ICollectionToGenericCollectionAdapter _values;
107 
108  private ICollectionToGenericCollectionAdapter _keys;
109 
110  public int Count => m_stringDictionary.Count;
111 
112  public string this[string key]
113  {
114  get
115  {
116  if (key == null)
117  {
118  throw new ArgumentNullException("key");
119  }
120  if (!m_stringDictionary.ContainsKey(key))
121  {
122  throw new KeyNotFoundException();
123  }
124  return m_stringDictionary[key];
125  }
126  set
127  {
128  if (key == null)
129  {
130  throw new ArgumentNullException("key");
131  }
132  m_stringDictionary[key] = value;
133  }
134  }
135 
137  {
138  get
139  {
140  if (_keys == null)
141  {
142  _keys = new ICollectionToGenericCollectionAdapter(m_stringDictionary, KeyOrValue.Key);
143  }
144  return _keys;
145  }
146  }
147 
149  {
150  get
151  {
152  if (_values == null)
153  {
154  _values = new ICollectionToGenericCollectionAdapter(m_stringDictionary, KeyOrValue.Value);
155  }
156  return _values;
157  }
158  }
159 
161  {
162  get
163  {
164  return false;
165  }
166  }
167 
168  internal GenericAdapter(StringDictionary stringDictionary)
169  {
170  m_stringDictionary = stringDictionary;
171  }
172 
173  public void Add(string key, string value)
174  {
175  this[key] = value;
176  }
177 
178  public bool ContainsKey(string key)
179  {
180  return m_stringDictionary.ContainsKey(key);
181  }
182 
183  public void Clear()
184  {
185  m_stringDictionary.Clear();
186  }
187 
188  public bool Remove(string key)
189  {
190  if (!m_stringDictionary.ContainsKey(key))
191  {
192  return false;
193  }
194  m_stringDictionary.Remove(key);
195  return true;
196  }
197 
198  public bool TryGetValue(string key, out string value)
199  {
200  if (!m_stringDictionary.ContainsKey(key))
201  {
202  value = null;
203  return false;
204  }
205  value = m_stringDictionary[key];
206  return true;
207  }
208 
210  {
211  m_stringDictionary.Add(item.Key, item.Value);
212  }
213 
215  {
216  if (TryGetValue(item.Key, out string value))
217  {
218  return value.Equals(item.Value);
219  }
220  return false;
221  }
222 
224  {
225  if (array == null)
226  {
227  throw new ArgumentNullException("array", SR.GetString("ArgumentNull_Array"));
228  }
229  if (arrayIndex < 0)
230  {
231  throw new ArgumentOutOfRangeException("arrayIndex", SR.GetString("ArgumentOutOfRange_NeedNonNegNum"));
232  }
233  if (array.Length - arrayIndex < Count)
234  {
235  throw new ArgumentException(SR.GetString("Arg_ArrayPlusOffTooSmall"));
236  }
237  int num = arrayIndex;
238  foreach (DictionaryEntry item in m_stringDictionary)
239  {
240  array[num++] = new KeyValuePair<string, string>((string)item.Key, (string)item.Value);
241  }
242  }
243 
245  {
246  if (!((ICollection<KeyValuePair<string, string>>)this).Contains(item))
247  {
248  return false;
249  }
250  m_stringDictionary.Remove(item.Key);
251  return true;
252  }
253 
255  {
256  return GetEnumerator();
257  }
258 
260  {
261  foreach (DictionaryEntry item in m_stringDictionary)
262  {
263  yield return new KeyValuePair<string, string>((string)item.Key, (string)item.Value);
264  }
265  }
266  }
267 
268  internal Hashtable contents = new Hashtable();
269 
272  public virtual int Count => contents.Count;
273 
277  public virtual bool IsSynchronized => contents.IsSynchronized;
278 
284  public virtual string this[string key]
285  {
286  get
287  {
288  if (key == null)
289  {
290  throw new ArgumentNullException("key");
291  }
292  return (string)contents[key.ToLower(CultureInfo.InvariantCulture)];
293  }
294  set
295  {
296  if (key == null)
297  {
298  throw new ArgumentNullException("key");
299  }
300  contents[key.ToLower(CultureInfo.InvariantCulture)] = value;
301  }
302  }
303 
306  public virtual ICollection Keys => contents.Keys;
307 
310  public virtual object SyncRoot => contents.SyncRoot;
311 
314  public virtual ICollection Values => contents.Values;
315 
323  public virtual void Add(string key, string value)
324  {
325  if (key == null)
326  {
327  throw new ArgumentNullException("key");
328  }
329  contents.Add(key.ToLower(CultureInfo.InvariantCulture), value);
330  }
331 
334  public virtual void Clear()
335  {
336  contents.Clear();
337  }
338 
344  public virtual bool ContainsKey(string key)
345  {
346  if (key == null)
347  {
348  throw new ArgumentNullException("key");
349  }
350  return contents.ContainsKey(key.ToLower(CultureInfo.InvariantCulture));
351  }
352 
357  public virtual bool ContainsValue(string value)
358  {
359  return contents.ContainsValue(value);
360  }
361 
371  public virtual void CopyTo(Array array, int index)
372  {
373  contents.CopyTo(array, index);
374  }
375 
378  public virtual IEnumerator GetEnumerator()
379  {
380  return contents.GetEnumerator();
381  }
382 
387  public virtual void Remove(string key)
388  {
389  if (key == null)
390  {
391  throw new ArgumentNullException("key");
392  }
393  contents.Remove(key.ToLower(CultureInfo.InvariantCulture));
394  }
395 
396  internal void ReplaceHashtable(Hashtable useThisHashtableInstead)
397  {
398  contents = useThisHashtableInstead;
399  }
400 
401  internal IDictionary<string, string> AsGenericDictionary()
402  {
403  return new GenericAdapter(this);
404  }
405  }
406 }
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
virtual bool IsSynchronized
Gets a value indicating whether access to the T:System.Collections.Hashtable is synchronized (thread ...
Definition: Hashtable.cs:612
virtual ICollection Values
Gets a collection of values in the T:System.Collections.Specialized.StringDictionary.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
virtual void Add(object key, object value)
Adds an element with the specified key and value into the T:System.Collections.Hashtable.
Definition: Hashtable.cs:916
virtual void Add(string key, string value)
Adds an entry with the specified key and value into the T:System.Collections.Specialized....
virtual object SyncRoot
Gets an object that can be used to synchronize access to the T:System.Collections....
virtual ICollection Keys
Gets a collection of keys in the T:System.Collections.Specialized.StringDictionary.
virtual void Remove(string key)
Removes the entry with the specified key from the string dictionary.
Implements a hash table with the key and the value strongly typed to be strings rather than objects.
Definition: __Canon.cs:3
virtual IEnumerator GetEnumerator()
Returns an enumerator that iterates through the string dictionary.
The exception that is thrown when the value of an argument is outside the allowable range of values a...
virtual bool IsSynchronized
Gets a value indicating whether access to the T:System.Collections.Specialized.StringDictionary is sy...
virtual bool ContainsKey(object key)
Determines whether the T:System.Collections.Hashtable contains a specific key.
Definition: Hashtable.cs:983
The exception that is thrown when the key specified for accessing an element in a collection does not...
virtual ICollection Values
Gets an T:System.Collections.ICollection containing the values in the T:System.Collections....
Definition: Hashtable.cs:631
Defines a key/value pair that can be set or retrieved.
Definition: KeyValuePair.cs:10
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
virtual void CopyTo(Array array, int arrayIndex)
Copies the T:System.Collections.Hashtable elements to a one-dimensional T:System.Array instance at th...
Definition: Hashtable.cs:1084
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
virtual ICollection Keys
Gets an T:System.Collections.ICollection containing the keys in the T:System.Collections....
Definition: Hashtable.cs:617
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
virtual int Count
Gets the number of key/value pairs in the T:System.Collections.Specialized.StringDictionary.
virtual bool ContainsValue(string value)
Determines if the T:System.Collections.Specialized.StringDictionary contains a specific value.
virtual void CopyTo(Array array, int index)
Copies the string dictionary values to a one-dimensional T:System.Array instance at the specified ind...
IEnumerator GetEnumerator()
Returns an enumerator that iterates through a collection.
virtual bool ContainsValue(object value)
Determines whether the T:System.Collections.Hashtable contains a specific value.
Definition: Hashtable.cs:1017
virtual object SyncRoot
Gets an object that can be used to synchronize access to the T:System.Collections....
Definition: Hashtable.cs:645
virtual void Clear()
Removes all entries from the T:System.Collections.Specialized.StringDictionary.
The exception that is thrown when one of the arguments provided to a method is not valid.
virtual bool ContainsKey(string key)
Determines if the T:System.Collections.Specialized.StringDictionary contains a specific key.
virtual void Remove(object key)
Removes the element with the specified key from the T:System.Collections.Hashtable.
Definition: Hashtable.cs:1349
Specifies that the class can be serialized.
virtual void Clear()
Removes all elements from the T:System.Collections.Hashtable.
Definition: Hashtable.cs:924
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
The exception that is thrown when an invoked method is not supported, or when there is an attempt to ...
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
void CopyTo(Array array, int index)
Copies the elements of the T:System.Collections.ICollection to an T:System.Array, starting at a parti...
Defines a dictionary key/value pair that can be set or retrieved.
Represents a nongeneric collection of key/value pairs.
Definition: IDictionary.cs:8
virtual int Count
Gets the number of key/value pairs contained in the T:System.Collections.Hashtable.
Definition: Hashtable.cs:658