mscorlib(4.0.0.0) API with additions
NameObjectCollectionBase.cs
4 using System.Threading;
5 
7 {
11  {
12  internal class NameObjectEntry
13  {
14  internal string Key;
15 
16  internal object Value;
17 
18  internal NameObjectEntry(string name, object value)
19  {
20  Key = name;
21  Value = value;
22  }
23  }
24 
25  [Serializable]
26  internal class NameObjectKeysEnumerator : IEnumerator
27  {
28  private int _pos;
29 
30  private NameObjectCollectionBase _coll;
31 
32  private int _version;
33 
34  public object Current
35  {
36  get
37  {
38  if (_pos >= 0 && _pos < _coll.Count)
39  {
40  return _coll.BaseGetKey(_pos);
41  }
42  throw new InvalidOperationException(SR.GetString("InvalidOperation_EnumOpCantHappen"));
43  }
44  }
45 
46  internal NameObjectKeysEnumerator(NameObjectCollectionBase coll)
47  {
48  _coll = coll;
49  _version = _coll._version;
50  _pos = -1;
51  }
52 
53  public bool MoveNext()
54  {
55  if (_version != _coll._version)
56  {
57  throw new InvalidOperationException(SR.GetString("InvalidOperation_EnumFailedVersion"));
58  }
59  if (_pos < _coll.Count - 1)
60  {
61  _pos++;
62  return true;
63  }
64  _pos = _coll.Count;
65  return false;
66  }
67 
68  public void Reset()
69  {
70  if (_version != _coll._version)
71  {
72  throw new InvalidOperationException(SR.GetString("InvalidOperation_EnumFailedVersion"));
73  }
74  _pos = -1;
75  }
76  }
77 
79  [Serializable]
81  {
82  private NameObjectCollectionBase _coll;
83 
89  public string this[int index]
90  {
91  get
92  {
93  return Get(index);
94  }
95  }
96 
99  public int Count => _coll.Count;
100 
103  object ICollection.SyncRoot
104  {
105  get
106  {
107  return ((ICollection)_coll).SyncRoot;
108  }
109  }
110 
115  {
116  get
117  {
118  return false;
119  }
120  }
121 
122  internal KeysCollection(NameObjectCollectionBase coll)
123  {
124  _coll = coll;
125  }
126 
132  public virtual string Get(int index)
133  {
134  return _coll.BaseGetKey(index);
135  }
136 
140  {
141  return new NameObjectKeysEnumerator(_coll);
142  }
143 
154  void ICollection.CopyTo(Array array, int index)
155  {
156  if (array == null)
157  {
158  throw new ArgumentNullException("array");
159  }
160  if (array.Rank != 1)
161  {
162  throw new ArgumentException(SR.GetString("Arg_MultiRank"));
163  }
164  if (index < 0)
165  {
166  throw new ArgumentOutOfRangeException("index", SR.GetString("IndexOutOfRange", index.ToString(CultureInfo.CurrentCulture)));
167  }
168  if (array.Length - index < _coll.Count)
169  {
170  throw new ArgumentException(SR.GetString("Arg_InsufficientSpace"));
171  }
172  IEnumerator enumerator = GetEnumerator();
173  while (enumerator.MoveNext())
174  {
175  array.SetValue(enumerator.Current, index++);
176  }
177  }
178  }
179 
180  private const string ReadOnlyName = "ReadOnly";
181 
182  private const string CountName = "Count";
183 
184  private const string ComparerName = "Comparer";
185 
186  private const string HashCodeProviderName = "HashProvider";
187 
188  private const string KeysName = "Keys";
189 
190  private const string ValuesName = "Values";
191 
192  private const string KeyComparerName = "KeyComparer";
193 
194  private const string VersionName = "Version";
195 
196  private bool _readOnly;
197 
198  private ArrayList _entriesArray;
199 
200  private IEqualityComparer _keyComparer;
201 
202  private volatile Hashtable _entriesTable;
203 
204  private volatile NameObjectEntry _nullKeyEntry;
205 
206  private KeysCollection _keys;
207 
208  private SerializationInfo _serializationInfo;
209 
210  private int _version;
211 
212  [NonSerialized]
213  private object _syncRoot;
214 
215  private static StringComparer defaultComparer = StringComparer.InvariantCultureIgnoreCase;
216 
217  internal IEqualityComparer Comparer
218  {
219  get
220  {
221  return _keyComparer;
222  }
223  set
224  {
225  _keyComparer = value;
226  }
227  }
228 
232  protected bool IsReadOnly
233  {
234  get
235  {
236  return _readOnly;
237  }
238  set
239  {
240  _readOnly = value;
241  }
242  }
243 
246  public virtual int Count => _entriesArray.Count;
247 
250  object ICollection.SyncRoot
251  {
252  get
253  {
254  if (_syncRoot == null)
255  {
256  Interlocked.CompareExchange(ref _syncRoot, new object(), null);
257  }
258  return _syncRoot;
259  }
260  }
261 
266  {
267  get
268  {
269  return false;
270  }
271  }
272 
275  public virtual KeysCollection Keys
276  {
277  get
278  {
279  if (_keys == null)
280  {
281  _keys = new KeysCollection(this);
282  }
283  return _keys;
284  }
285  }
286 
289  : this(defaultComparer)
290  {
291  }
292 
295  protected NameObjectCollectionBase(IEqualityComparer equalityComparer)
296  {
297  object keyComparer;
298  if (equalityComparer != null)
299  {
300  keyComparer = equalityComparer;
301  }
302  else
303  {
304  IEqualityComparer equalityComparer2 = defaultComparer;
305  keyComparer = equalityComparer2;
306  }
307  _keyComparer = (IEqualityComparer)keyComparer;
308  Reset();
309  }
310 
316  protected NameObjectCollectionBase(int capacity, IEqualityComparer equalityComparer)
317  : this(equalityComparer)
318  {
319  Reset(capacity);
320  }
321 
325  [Obsolete("Please use NameObjectCollectionBase(IEqualityComparer) instead.")]
326  protected NameObjectCollectionBase(IHashCodeProvider hashProvider, IComparer comparer)
327  {
328  _keyComparer = new CompatibleComparer(comparer, hashProvider);
329  Reset();
330  }
331 
338  [Obsolete("Please use NameObjectCollectionBase(Int32, IEqualityComparer) instead.")]
339  protected NameObjectCollectionBase(int capacity, IHashCodeProvider hashProvider, IComparer comparer)
340  {
341  _keyComparer = new CompatibleComparer(comparer, hashProvider);
342  Reset(capacity);
343  }
344 
349  protected NameObjectCollectionBase(int capacity)
350  {
352  Reset(capacity);
353  }
354 
355  internal NameObjectCollectionBase(DBNull dummy)
356  {
357  }
358 
363  {
364  _serializationInfo = info;
365  }
366 
372  [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
373  public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
374  {
375  if (info == null)
376  {
377  throw new ArgumentNullException("info");
378  }
379  info.AddValue("ReadOnly", _readOnly);
380  if (_keyComparer == defaultComparer)
381  {
382  info.AddValue("HashProvider", CompatibleComparer.DefaultHashCodeProvider, typeof(IHashCodeProvider));
383  info.AddValue("Comparer", CompatibleComparer.DefaultComparer, typeof(IComparer));
384  }
385  else if (_keyComparer == null)
386  {
387  info.AddValue("HashProvider", null, typeof(IHashCodeProvider));
388  info.AddValue("Comparer", null, typeof(IComparer));
389  }
390  else if (_keyComparer is CompatibleComparer)
391  {
392  CompatibleComparer compatibleComparer = (CompatibleComparer)_keyComparer;
393  info.AddValue("HashProvider", compatibleComparer.HashCodeProvider, typeof(IHashCodeProvider));
394  info.AddValue("Comparer", compatibleComparer.Comparer, typeof(IComparer));
395  }
396  else
397  {
398  info.AddValue("KeyComparer", _keyComparer, typeof(IEqualityComparer));
399  }
400  int count = _entriesArray.Count;
401  info.AddValue("Count", count);
402  string[] array = new string[count];
403  object[] array2 = new object[count];
404  for (int i = 0; i < count; i++)
405  {
406  NameObjectEntry nameObjectEntry = (NameObjectEntry)_entriesArray[i];
407  array[i] = nameObjectEntry.Key;
408  array2[i] = nameObjectEntry.Value;
409  }
410  info.AddValue("Keys", array, typeof(string[]));
411  info.AddValue("Values", array2, typeof(object[]));
412  info.AddValue("Version", _version);
413  }
414 
418  public virtual void OnDeserialization(object sender)
419  {
420  if (_keyComparer != null)
421  {
422  return;
423  }
424  if (_serializationInfo == null)
425  {
426  throw new SerializationException();
427  }
428  SerializationInfo serializationInfo = _serializationInfo;
429  _serializationInfo = null;
430  bool readOnly = false;
431  int num = 0;
432  string[] array = null;
433  object[] array2 = null;
434  IHashCodeProvider hashCodeProvider = null;
435  IComparer comparer = null;
436  bool flag = false;
437  int version = 0;
438  SerializationInfoEnumerator enumerator = serializationInfo.GetEnumerator();
439  while (enumerator.MoveNext())
440  {
441  switch (enumerator.Name)
442  {
443  case "ReadOnly":
444  readOnly = serializationInfo.GetBoolean("ReadOnly");
445  break;
446  case "HashProvider":
447  hashCodeProvider = (IHashCodeProvider)serializationInfo.GetValue("HashProvider", typeof(IHashCodeProvider));
448  break;
449  case "Comparer":
450  comparer = (IComparer)serializationInfo.GetValue("Comparer", typeof(IComparer));
451  break;
452  case "KeyComparer":
453  _keyComparer = (IEqualityComparer)serializationInfo.GetValue("KeyComparer", typeof(IEqualityComparer));
454  break;
455  case "Count":
456  num = serializationInfo.GetInt32("Count");
457  break;
458  case "Keys":
459  array = (string[])serializationInfo.GetValue("Keys", typeof(string[]));
460  break;
461  case "Values":
462  array2 = (object[])serializationInfo.GetValue("Values", typeof(object[]));
463  break;
464  case "Version":
465  flag = true;
466  version = serializationInfo.GetInt32("Version");
467  break;
468  }
469  }
470  if (_keyComparer == null)
471  {
472  if (comparer == null || hashCodeProvider == null)
473  {
474  throw new SerializationException();
475  }
476  _keyComparer = new CompatibleComparer(comparer, hashCodeProvider);
477  }
478  if (array == null || array2 == null)
479  {
480  throw new SerializationException();
481  }
482  Reset(num);
483  for (int i = 0; i < num; i++)
484  {
485  BaseAdd(array[i], array2[i]);
486  }
487  _readOnly = readOnly;
488  if (flag)
489  {
490  _version = version;
491  }
492  }
493 
494  private void Reset()
495  {
496  _entriesArray = new ArrayList();
497  _entriesTable = new Hashtable(_keyComparer);
498  _nullKeyEntry = null;
499  _version++;
500  }
501 
502  private void Reset(int capacity)
503  {
504  _entriesArray = new ArrayList(capacity);
505  _entriesTable = new Hashtable(capacity, _keyComparer);
506  _nullKeyEntry = null;
507  _version++;
508  }
509 
510  private NameObjectEntry FindEntry(string key)
511  {
512  if (key != null)
513  {
514  return (NameObjectEntry)_entriesTable[key];
515  }
516  return _nullKeyEntry;
517  }
518 
522  protected bool BaseHasKeys()
523  {
524  return _entriesTable.Count > 0;
525  }
526 
531  protected void BaseAdd(string name, object value)
532  {
533  if (_readOnly)
534  {
535  throw new NotSupportedException(SR.GetString("CollectionReadOnly"));
536  }
537  NameObjectEntry nameObjectEntry = new NameObjectEntry(name, value);
538  if (name != null)
539  {
540  if (_entriesTable[name] == null)
541  {
542  _entriesTable.Add(name, nameObjectEntry);
543  }
544  }
545  else if (_nullKeyEntry == null)
546  {
547  _nullKeyEntry = nameObjectEntry;
548  }
549  _entriesArray.Add(nameObjectEntry);
550  _version++;
551  }
552 
556  protected void BaseRemove(string name)
557  {
558  if (_readOnly)
559  {
560  throw new NotSupportedException(SR.GetString("CollectionReadOnly"));
561  }
562  if (name != null)
563  {
564  _entriesTable.Remove(name);
565  for (int num = _entriesArray.Count - 1; num >= 0; num--)
566  {
567  if (_keyComparer.Equals(name, BaseGetKey(num)))
568  {
569  _entriesArray.RemoveAt(num);
570  }
571  }
572  }
573  else
574  {
575  _nullKeyEntry = null;
576  for (int num2 = _entriesArray.Count - 1; num2 >= 0; num2--)
577  {
578  if (BaseGetKey(num2) == null)
579  {
580  _entriesArray.RemoveAt(num2);
581  }
582  }
583  }
584  _version++;
585  }
586 
592  protected void BaseRemoveAt(int index)
593  {
594  if (_readOnly)
595  {
596  throw new NotSupportedException(SR.GetString("CollectionReadOnly"));
597  }
598  string text = BaseGetKey(index);
599  if (text != null)
600  {
601  _entriesTable.Remove(text);
602  }
603  else
604  {
605  _nullKeyEntry = null;
606  }
607  _entriesArray.RemoveAt(index);
608  _version++;
609  }
610 
613  protected void BaseClear()
614  {
615  if (_readOnly)
616  {
617  throw new NotSupportedException(SR.GetString("CollectionReadOnly"));
618  }
619  Reset();
620  }
621 
625  protected object BaseGet(string name)
626  {
627  return FindEntry(name)?.Value;
628  }
629 
634  protected void BaseSet(string name, object value)
635  {
636  if (_readOnly)
637  {
638  throw new NotSupportedException(SR.GetString("CollectionReadOnly"));
639  }
640  NameObjectEntry nameObjectEntry = FindEntry(name);
641  if (nameObjectEntry != null)
642  {
643  nameObjectEntry.Value = value;
644  _version++;
645  }
646  else
647  {
648  BaseAdd(name, value);
649  }
650  }
651 
657  protected object BaseGet(int index)
658  {
659  NameObjectEntry nameObjectEntry = (NameObjectEntry)_entriesArray[index];
660  return nameObjectEntry.Value;
661  }
662 
668  protected string BaseGetKey(int index)
669  {
670  NameObjectEntry nameObjectEntry = (NameObjectEntry)_entriesArray[index];
671  return nameObjectEntry.Key;
672  }
673 
680  protected void BaseSet(int index, object value)
681  {
682  if (_readOnly)
683  {
684  throw new NotSupportedException(SR.GetString("CollectionReadOnly"));
685  }
686  NameObjectEntry nameObjectEntry = (NameObjectEntry)_entriesArray[index];
687  nameObjectEntry.Value = value;
688  _version++;
689  }
690 
693  public virtual IEnumerator GetEnumerator()
694  {
695  return new NameObjectKeysEnumerator(this);
696  }
697 
708  void ICollection.CopyTo(Array array, int index)
709  {
710  if (array == null)
711  {
712  throw new ArgumentNullException("array");
713  }
714  if (array.Rank != 1)
715  {
716  throw new ArgumentException(SR.GetString("Arg_MultiRank"));
717  }
718  if (index < 0)
719  {
720  throw new ArgumentOutOfRangeException("index", SR.GetString("IndexOutOfRange", index.ToString(CultureInfo.CurrentCulture)));
721  }
722  if (array.Length - index < _entriesArray.Count)
723  {
724  throw new ArgumentException(SR.GetString("Arg_InsufficientSpace"));
725  }
726  IEnumerator enumerator = GetEnumerator();
727  while (enumerator.MoveNext())
728  {
729  array.SetValue(enumerator.Current, index++);
730  }
731  }
732 
735  protected string[] BaseGetAllKeys()
736  {
737  int count = _entriesArray.Count;
738  string[] array = new string[count];
739  for (int i = 0; i < count; i++)
740  {
741  array[i] = BaseGetKey(i);
742  }
743  return array;
744  }
745 
748  protected object[] BaseGetAllValues()
749  {
750  int count = _entriesArray.Count;
751  object[] array = new object[count];
752  for (int i = 0; i < count; i++)
753  {
754  array[i] = BaseGet(i);
755  }
756  return array;
757  }
758 
766  protected object[] BaseGetAllValues(Type type)
767  {
768  int count = _entriesArray.Count;
769  if (type == null)
770  {
771  throw new ArgumentNullException("type");
772  }
773  object[] array = (object[])SecurityUtils.ArrayCreateInstance(type, count);
774  for (int i = 0; i < count; i++)
775  {
776  array[i] = BaseGet(i);
777  }
778  return array;
779  }
780  }
781 }
Supplies a hash code for an object, using a custom hash function.
object SyncRoot
Gets an object that can be used to synchronize access to the T:System.Collections....
Definition: ICollection.cs:23
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
NameObjectCollectionBase(SerializationInfo info, StreamingContext context)
Initializes a new instance of the T:System.Collections.Specialized.NameObjectCollectionBase class tha...
void BaseRemoveAt(int index)
Removes the entry at the specified index of the T:System.Collections.Specialized.NameObjectCollection...
object [] BaseGetAllValues()
Returns an T:System.Object array that contains all the values in the T:System.Collections....
Indicates that a class is to be notified when deserialization of the entire object graph has been com...
bool GetBoolean(string name)
Retrieves a Boolean value from the T:System.Runtime.Serialization.SerializationInfo store.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
SerializationInfoEnumerator GetEnumerator()
Returns a T:System.Runtime.Serialization.SerializationInfoEnumerator used to iterate through the name...
IEnumerator GetEnumerator()
Returns an enumerator that iterates through the T:System.Collections.Specialized.NameObjectCollection...
NameObjectCollectionBase(IHashCodeProvider hashProvider, IComparer comparer)
Initializes a new instance of the T:System.Collections.Specialized.NameObjectCollectionBase class tha...
Describes the source and destination of a given serialized stream, and provides an additional caller-...
NameObjectCollectionBase(int capacity, IEqualityComparer equalityComparer)
Initializes a new instance of the T:System.Collections.Specialized.NameObjectCollectionBase class tha...
SecurityAction
Specifies the security actions that can be performed using declarative security.
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
void BaseClear()
Removes all entries from the T:System.Collections.Specialized.NameObjectCollectionBase instance.
object [] BaseGetAllValues(Type type)
Returns an array of the specified type that contains all the values in the T:System....
string [] BaseGetAllKeys()
Returns a T:System.String array that contains all the keys in the T:System.Collections....
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
Defines methods to support the comparison of objects for equality.
virtual IEnumerator GetEnumerator()
Returns an enumerator that iterates through the T:System.Collections.Specialized.NameObjectCollection...
Exposes a method that compares two objects.
Definition: IComparer.cs:8
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
bool IsSynchronized
Gets a value indicating whether access to the T:System.Collections.ICollection is synchronized (threa...
Definition: ICollection.cs:33
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
Represents a collection of the T:System.String keys of a collection.
virtual void GetObjectData(SerializationInfo info, StreamingContext context)
Implements the T:System.Runtime.Serialization.ISerializable interface and returns the data needed to ...
NameObjectCollectionBase(int capacity)
Initializes a new instance of the T:System.Collections.Specialized.NameObjectCollectionBase class tha...
The exception thrown when an error occurs during serialization or deserialization.
string BaseGetKey(int index)
Gets the key of the entry at the specified index of the T:System.Collections.Specialized....
virtual KeysCollection Keys
Gets a T:System.Collections.Specialized.NameObjectCollectionBase.KeysCollection instance that contain...
Provides the abstract base class for a collection of associated T:System.String keys and T:System....
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
static StringComparer InvariantCultureIgnoreCase
Gets a T:System.StringComparer object that performs a case-insensitive string comparison using the wo...
NameObjectCollectionBase(int capacity, IHashCodeProvider hashProvider, IComparer comparer)
Initializes a new instance of the T:System.Collections.Specialized.NameObjectCollectionBase class tha...
static CultureInfo CurrentCulture
Gets or sets the T:System.Globalization.CultureInfo object that represents the culture used by the cu...
Definition: CultureInfo.cs:120
The exception that is thrown when one of the arguments provided to a method is not valid.
object BaseGet(int index)
Gets the value of the entry at the specified index of the T:System.Collections.Specialized....
virtual void OnDeserialization(object sender)
Implements the T:System.Runtime.Serialization.ISerializable interface and raises the deserialization ...
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
int Count
Gets the number of keys in the T:System.Collections.Specialized.NameObjectCollectionBase....
object BaseGet(string name)
Gets the value of the first entry with the specified key from the T:System.Collections....
object GetValue(string name, Type type)
Retrieves a value from the T:System.Runtime.Serialization.SerializationInfo store.
Specifies that the class can be serialized.
string Name
Gets the name for the item currently being examined.
virtual int Count
Gets the number of key/value pairs contained in the T:System.Collections.Specialized....
The exception that is thrown when a method call is invalid for the object's current state.
int GetInt32(string name)
Retrieves a 32-bit signed integer value from the T:System.Runtime.Serialization.SerializationInfo sto...
NameObjectCollectionBase()
Initializes a new instance of the T:System.Collections.Specialized.NameObjectCollectionBase class tha...
void BaseSet(string name, object value)
Sets the value of the first entry with the specified key in the T:System.Collections....
virtual string Get(int index)
Gets the key at the specified index of the collection.
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
SecurityPermissionFlag
Specifies access flags for the security permission object.
bool BaseHasKeys()
Gets a value indicating whether the T:System.Collections.Specialized.NameObjectCollectionBase instanc...
void CopyTo(Array array, int index)
Copies the elements of the T:System.Collections.ICollection to an T:System.Array, starting at a parti...
void BaseRemove(string name)
Removes the entries with the specified key from the T:System.Collections.Specialized....
Provides a formatter-friendly mechanism for parsing the data in T:System.Runtime.Serialization....
void BaseSet(int index, object value)
Sets the value of the entry at the specified index of the T:System.Collections.Specialized....
void BaseAdd(string name, object value)
Adds an entry with the specified key and value into the T:System.Collections.Specialized....
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
bool IsReadOnly
Gets or sets a value indicating whether the T:System.Collections.Specialized.NameObjectCollectionBase...
Represents a string comparison operation that uses specific case and culture-based or ordinal compari...
Represents a nonexistent value. This class cannot be inherited.
Definition: DBNull.cs:10
NameObjectCollectionBase(IEqualityComparer equalityComparer)
Initializes a new instance of the T:System.Collections.Specialized.NameObjectCollectionBase class tha...
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14
bool MoveNext()
Updates the enumerator to the next item.