mscorlib(4.0.0.0) API with additions
PropertyDescriptorCollection.cs
1 using System.Collections;
4 
5 namespace System.ComponentModel
6 {
8  [HostProtection(SecurityAction.LinkDemand, Synchronization = true)]
10  {
11  private class PropertyDescriptorEnumerator : IDictionaryEnumerator, IEnumerator
12  {
13  private PropertyDescriptorCollection owner;
14 
15  private int index = -1;
16 
17  public object Current => Entry;
18 
19  public DictionaryEntry Entry
20  {
21  get
22  {
23  PropertyDescriptor propertyDescriptor = owner[index];
24  return new DictionaryEntry(propertyDescriptor.Name, propertyDescriptor);
25  }
26  }
27 
28  public object Key => owner[index].Name;
29 
30  public object Value => owner[index].Name;
31 
32  public PropertyDescriptorEnumerator(PropertyDescriptorCollection owner)
33  {
34  this.owner = owner;
35  }
36 
37  public bool MoveNext()
38  {
39  if (index < owner.Count - 1)
40  {
41  index++;
42  return true;
43  }
44  return false;
45  }
46 
47  public void Reset()
48  {
49  index = -1;
50  }
51  }
52 
54  public static readonly PropertyDescriptorCollection Empty = new PropertyDescriptorCollection(null, readOnly: true);
55 
56  private IDictionary cachedFoundProperties;
57 
58  private bool cachedIgnoreCase;
59 
60  private PropertyDescriptor[] properties;
61 
62  private int propCount;
63 
64  private string[] namedSort;
65 
66  private IComparer comparer;
67 
68  private bool propsOwned = true;
69 
70  private bool needSort;
71 
72  private bool readOnly;
73 
76  public int Count => propCount;
77 
82  public virtual PropertyDescriptor this[int index]
83  {
84  get
85  {
86  if (index >= propCount)
87  {
88  throw new IndexOutOfRangeException();
89  }
90  EnsurePropsOwned();
91  return properties[index];
92  }
93  }
94 
98  public virtual PropertyDescriptor this[string name]
99  {
100  get
101  {
102  return Find(name, ignoreCase: false);
103  }
104  }
105 
108  int ICollection.Count
109  {
110  get
111  {
112  return Count;
113  }
114  }
115 
120  {
121  get
122  {
123  return false;
124  }
125  }
126 
129  object ICollection.SyncRoot
130  {
131  get
132  {
133  return null;
134  }
135  }
136 
141  {
142  get
143  {
144  return readOnly;
145  }
146  }
147 
152  {
153  get
154  {
155  return readOnly;
156  }
157  }
158 
162  object IDictionary.this[object key]
163  {
164  get
165  {
166  if (key is string)
167  {
168  return this[(string)key];
169  }
170  return null;
171  }
172  set
173  {
174  if (readOnly)
175  {
176  throw new NotSupportedException();
177  }
178  if (value != null && !(value is PropertyDescriptor))
179  {
180  throw new ArgumentException("value");
181  }
182  int num = -1;
183  if (key is int)
184  {
185  num = (int)key;
186  if (num < 0 || num >= propCount)
187  {
188  throw new IndexOutOfRangeException();
189  }
190  }
191  else
192  {
193  if (!(key is string))
194  {
195  throw new ArgumentException("key");
196  }
197  for (int i = 0; i < propCount; i++)
198  {
199  if (properties[i].Name.Equals((string)key))
200  {
201  num = i;
202  break;
203  }
204  }
205  }
206  if (num == -1)
207  {
208  Add((PropertyDescriptor)value);
209  return;
210  }
211  EnsurePropsOwned();
212  properties[num] = (PropertyDescriptor)value;
213  if (cachedFoundProperties != null && key is string)
214  {
215  cachedFoundProperties[key] = value;
216  }
217  }
218  }
219 
223  {
224  get
225  {
226  string[] array = new string[propCount];
227  for (int i = 0; i < propCount; i++)
228  {
229  array[i] = properties[i].Name;
230  }
231  return array;
232  }
233  }
234 
238  {
239  get
240  {
241  if (properties.Length != propCount)
242  {
243  PropertyDescriptor[] array = new PropertyDescriptor[propCount];
244  Array.Copy(properties, 0, array, 0, propCount);
245  return array;
246  }
247  return (ICollection)properties.Clone();
248  }
249  }
250 
254  bool IList.IsReadOnly
255  {
256  get
257  {
258  return readOnly;
259  }
260  }
261 
265  bool IList.IsFixedSize
266  {
267  get
268  {
269  return readOnly;
270  }
271  }
272 
282  object IList.this[int index]
283  {
284  get
285  {
286  return this[index];
287  }
288  set
289  {
290  if (readOnly)
291  {
292  throw new NotSupportedException();
293  }
294  if (index >= propCount)
295  {
296  throw new IndexOutOfRangeException();
297  }
298  if (value != null && !(value is PropertyDescriptor))
299  {
300  throw new ArgumentException("value");
301  }
302  EnsurePropsOwned();
303  properties[index] = (PropertyDescriptor)value;
304  }
305  }
306 
310  {
311  this.properties = properties;
312  if (properties == null)
313  {
314  this.properties = new PropertyDescriptor[0];
315  propCount = 0;
316  }
317  else
318  {
319  propCount = properties.Length;
320  }
321  propsOwned = true;
322  }
323 
327  public PropertyDescriptorCollection(PropertyDescriptor[] properties, bool readOnly)
328  : this(properties)
329  {
330  this.readOnly = readOnly;
331  }
332 
333  private PropertyDescriptorCollection(PropertyDescriptor[] properties, int propCount, string[] namedSort, IComparer comparer)
334  {
335  propsOwned = false;
336  if (namedSort != null)
337  {
338  this.namedSort = (string[])namedSort.Clone();
339  }
340  this.comparer = comparer;
341  this.properties = properties;
342  this.propCount = propCount;
343  needSort = true;
344  }
345 
350  public int Add(PropertyDescriptor value)
351  {
352  if (readOnly)
353  {
354  throw new NotSupportedException();
355  }
356  EnsureSize(propCount + 1);
357  properties[propCount++] = value;
358  return propCount - 1;
359  }
360 
363  public void Clear()
364  {
365  if (readOnly)
366  {
367  throw new NotSupportedException();
368  }
369  propCount = 0;
370  cachedFoundProperties = null;
371  }
372 
377  public bool Contains(PropertyDescriptor value)
378  {
379  return IndexOf(value) >= 0;
380  }
381 
385  public void CopyTo(Array array, int index)
386  {
387  EnsurePropsOwned();
388  Array.Copy(properties, 0, array, index, Count);
389  }
390 
391  private void EnsurePropsOwned()
392  {
393  if (!propsOwned)
394  {
395  propsOwned = true;
396  if (properties != null)
397  {
398  PropertyDescriptor[] destinationArray = new PropertyDescriptor[Count];
399  Array.Copy(properties, 0, destinationArray, 0, Count);
400  properties = destinationArray;
401  }
402  }
403  if (needSort)
404  {
405  needSort = false;
406  InternalSort(namedSort);
407  }
408  }
409 
410  private void EnsureSize(int sizeNeeded)
411  {
412  if (sizeNeeded > properties.Length)
413  {
414  if (properties == null || properties.Length == 0)
415  {
416  propCount = 0;
417  properties = new PropertyDescriptor[sizeNeeded];
418  return;
419  }
420  EnsurePropsOwned();
421  int num = Math.Max(sizeNeeded, properties.Length * 2);
422  PropertyDescriptor[] destinationArray = new PropertyDescriptor[num];
423  Array.Copy(properties, 0, destinationArray, 0, propCount);
424  properties = destinationArray;
425  }
426  }
427 
433  public virtual PropertyDescriptor Find(string name, bool ignoreCase)
434  {
435  lock (this)
436  {
437  PropertyDescriptor result = null;
438  if (cachedFoundProperties == null || cachedIgnoreCase != ignoreCase)
439  {
440  cachedIgnoreCase = ignoreCase;
441  cachedFoundProperties = new HybridDictionary(ignoreCase);
442  }
443  object obj = cachedFoundProperties[name];
444  if (obj != null)
445  {
446  return (PropertyDescriptor)obj;
447  }
448  for (int i = 0; i < propCount; i++)
449  {
450  if (ignoreCase)
451  {
452  if (string.Equals(properties[i].Name, name, StringComparison.OrdinalIgnoreCase))
453  {
454  cachedFoundProperties[name] = properties[i];
455  result = properties[i];
456  break;
457  }
458  }
459  else if (properties[i].Name.Equals(name))
460  {
461  cachedFoundProperties[name] = properties[i];
462  result = properties[i];
463  break;
464  }
465  }
466  return result;
467  }
468  }
469 
473  public int IndexOf(PropertyDescriptor value)
474  {
475  return Array.IndexOf(properties, value, 0, propCount);
476  }
477 
482  public void Insert(int index, PropertyDescriptor value)
483  {
484  if (readOnly)
485  {
486  throw new NotSupportedException();
487  }
488  EnsureSize(propCount + 1);
489  if (index < propCount)
490  {
491  Array.Copy(properties, index, properties, index + 1, propCount - index);
492  }
493  properties[index] = value;
494  propCount++;
495  }
496 
500  public void Remove(PropertyDescriptor value)
501  {
502  if (readOnly)
503  {
504  throw new NotSupportedException();
505  }
506  int num = IndexOf(value);
507  if (num != -1)
508  {
509  RemoveAt(num);
510  }
511  }
512 
516  public void RemoveAt(int index)
517  {
518  if (readOnly)
519  {
520  throw new NotSupportedException();
521  }
522  if (index < propCount - 1)
523  {
524  Array.Copy(properties, index + 1, properties, index, propCount - index - 1);
525  }
526  properties[propCount - 1] = null;
527  propCount--;
528  }
529 
533  {
534  return new PropertyDescriptorCollection(properties, propCount, namedSort, comparer);
535  }
536 
540  public virtual PropertyDescriptorCollection Sort(string[] names)
541  {
542  return new PropertyDescriptorCollection(properties, propCount, names, comparer);
543  }
544 
549  public virtual PropertyDescriptorCollection Sort(string[] names, IComparer comparer)
550  {
551  return new PropertyDescriptorCollection(properties, propCount, names, comparer);
552  }
553 
557  public virtual PropertyDescriptorCollection Sort(IComparer comparer)
558  {
559  return new PropertyDescriptorCollection(properties, propCount, namedSort, comparer);
560  }
561 
564  protected void InternalSort(string[] names)
565  {
566  if (properties == null || properties.Length == 0)
567  {
568  return;
569  }
570  InternalSort(comparer);
571  if (names == null || names.Length == 0)
572  {
573  return;
574  }
575  ArrayList arrayList = new ArrayList(properties);
576  int num = 0;
577  int num2 = properties.Length;
578  for (int i = 0; i < names.Length; i++)
579  {
580  for (int j = 0; j < num2; j++)
581  {
582  PropertyDescriptor propertyDescriptor = (PropertyDescriptor)arrayList[j];
583  if (propertyDescriptor != null && propertyDescriptor.Name.Equals(names[i]))
584  {
585  properties[num++] = propertyDescriptor;
586  arrayList[j] = null;
587  break;
588  }
589  }
590  }
591  for (int k = 0; k < num2; k++)
592  {
593  if (arrayList[k] != null)
594  {
595  properties[num++] = (PropertyDescriptor)arrayList[k];
596  }
597  }
598  }
599 
602  protected void InternalSort(IComparer sorter)
603  {
604  if (sorter == null)
605  {
607  }
608  else
609  {
610  Array.Sort(properties, sorter);
611  }
612  }
613 
616  public virtual IEnumerator GetEnumerator()
617  {
618  EnsurePropsOwned();
619  if (properties.Length != propCount)
620  {
621  PropertyDescriptor[] array = new PropertyDescriptor[propCount];
622  Array.Copy(properties, 0, array, 0, propCount);
623  return array.GetEnumerator();
624  }
625  return properties.GetEnumerator();
626  }
627 
634  void IDictionary.Add(object key, object value)
635  {
636  PropertyDescriptor propertyDescriptor = value as PropertyDescriptor;
637  if (propertyDescriptor == null)
638  {
639  throw new ArgumentException("value");
640  }
641  Add(propertyDescriptor);
642  }
643 
645  void IDictionary.Clear()
646  {
647  Clear();
648  }
649 
654  bool IDictionary.Contains(object key)
655  {
656  if (key is string)
657  {
658  return this[(string)key] != null;
659  }
660  return false;
661  }
662 
666  {
667  return new PropertyDescriptorEnumerator(this);
668  }
669 
672  void IDictionary.Remove(object key)
673  {
674  if (key is string)
675  {
676  PropertyDescriptor propertyDescriptor = this[(string)key];
677  if (propertyDescriptor != null)
678  {
679  ((IList)this).Remove((object)propertyDescriptor);
680  }
681  }
682  }
683 
687  {
688  return GetEnumerator();
689  }
690 
694  int IList.Add(object value)
695  {
696  return Add((PropertyDescriptor)value);
697  }
698 
701  void IList.Clear()
702  {
703  Clear();
704  }
705 
710  bool IList.Contains(object value)
711  {
712  return Contains((PropertyDescriptor)value);
713  }
714 
718  int IList.IndexOf(object value)
719  {
720  return IndexOf((PropertyDescriptor)value);
721  }
722 
727  void IList.Insert(int index, object value)
728  {
729  Insert(index, (PropertyDescriptor)value);
730  }
731 
735  void IList.Remove(object value)
736  {
737  Remove((PropertyDescriptor)value);
738  }
739 
743  void IList.RemoveAt(int index)
744  {
745  RemoveAt(index);
746  }
747  }
748 }
object SyncRoot
Gets an object that can be used to synchronize access to the T:System.Collections....
Definition: ICollection.cs:23
virtual IEnumerator GetEnumerator()
Returns an enumerator for this class.
static void SortDescriptorArray(IList infos)
Sorts descriptors using the name of the descriptor.
void Insert(int index, PropertyDescriptor value)
Adds the T:System.ComponentModel.PropertyDescriptor to the collection at the specified index number.
bool Contains(object key)
Determines whether the T:System.Collections.IDictionary object contains an element with the specified...
Much of the list has changed. Any listening controls should refresh all their data from the list.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
int Count
Gets the number of property descriptors in the collection.
Represents a non-generic collection of objects that can be individually accessed by index.
Definition: IList.cs:8
virtual PropertyDescriptor Find(string name, bool ignoreCase)
Returns the T:System.ComponentModel.PropertyDescriptor with the specified name, using a Boolean to in...
void RemoveAt(int index)
Removes the T:System.Collections.IList item at the specified index.
void Insert(int index, object value)
Inserts an item to the T:System.Collections.IList at the specified index.
Definition: __Canon.cs:3
bool IsReadOnly
Gets a value indicating whether the T:System.Collections.IList is read-only.
Definition: IList.cs:30
virtual PropertyDescriptorCollection Sort(IComparer comparer)
Sorts the members of this collection, using the specified T:System.Collections.IComparer.
void Clear()
Removes all items from the T:System.Collections.IList.
int IndexOf(object value)
Determines the index of a specific item in the T:System.Collections.IList.
void InternalSort(string[] names)
Sorts the members of this collection. The specified order is applied first, followed by the default s...
bool IsFixedSize
Gets a value indicating whether the T:System.Collections.IDictionary object has a fixed size.
Definition: IDictionary.cs:58
void CopyTo(Array array, int index)
Copies the entire collection to an array, starting at the specified index number.
Provides an abstraction of a property on a class.
virtual PropertyDescriptorCollection Sort(string[] names, IComparer comparer)
Sorts the members of this collection. The specified order is applied first, followed by the sort usin...
bool IsFixedSize
Gets a value indicating whether the T:System.Collections.IList has a fixed size.
Definition: IList.cs:40
virtual string Name
Gets the name of the member.
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
SecurityAction
Specifies the security actions that can be performed using declarative security.
static void Sort(Array array)
Sorts the elements in an entire one-dimensional T:System.Array using the T:System....
Definition: Array.cs:3259
static readonly PropertyDescriptorCollection Empty
Specifies an empty collection that you can use instead of creating a new one with no items....
The exception that is thrown when an attempt is made to access an element of an array or collection w...
PropertyDescriptorCollection(PropertyDescriptor[] properties)
Initializes a new instance of the T:System.ComponentModel.PropertyDescriptorCollection class.
void Add(object key, object value)
Adds an element with the provided key and value to the T:System.Collections.IDictionary object.
void InternalSort(IComparer sorter)
Sorts the members of this collection, using the specified T:System.Collections.IComparer.
PropertyDescriptorCollection(PropertyDescriptor[] properties, bool readOnly)
Initializes a new instance of the T:System.ComponentModel.PropertyDescriptorCollection class,...
void Clear()
Removes all T:System.ComponentModel.PropertyDescriptor objects from the collection.
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
void Clear()
Removes all elements from the T:System.Collections.IDictionary object.
Provides information about the characteristics for a component, such as its attributes,...
void Remove(object key)
Removes the element with the specified key from the T:System.Collections.IDictionary object.
int Add(object value)
Adds an item to the T:System.Collections.IList.
IEnumerator GetEnumerator()
Returns an enumerator that iterates through a collection.
The exception that is thrown when one of the arguments provided to a method is not valid.
ICollection Keys
Gets an T:System.Collections.ICollection object containing the keys of the T:System....
Definition: IDictionary.cs:29
ICollection Values
Gets an T:System.Collections.ICollection object containing the values in the T:System....
Definition: IDictionary.cs:38
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
Implements IDictionary by using a T:System.Collections.Specialized.ListDictionary while the collectio...
void Remove(PropertyDescriptor value)
Removes the specified T:System.ComponentModel.PropertyDescriptor from the collection.
new IDictionaryEnumerator GetEnumerator()
Returns an T:System.Collections.IDictionaryEnumerator object for the T:System.Collections....
bool IsReadOnly
Gets a value indicating whether the T:System.Collections.IDictionary object is read-only.
Definition: IDictionary.cs:48
Represents a collection of T:System.ComponentModel.PropertyDescriptor objects.
int Add(PropertyDescriptor value)
Adds the specified T:System.ComponentModel.PropertyDescriptor to the collection.
Enumerates the elements of a nongeneric dictionary.
virtual PropertyDescriptorCollection Sort(string[] names)
Sorts the members of this collection. The specified order is applied first, followed by the default s...
void Remove(object value)
Removes the first occurrence of a specific object from the T:System.Collections.IList.
void RemoveAt(int index)
Removes the T:System.ComponentModel.PropertyDescriptor at the specified index from the collection.
bool Contains(object value)
Determines whether the T:System.Collections.IList contains a specific value.
virtual PropertyDescriptorCollection Sort()
Sorts the members of this collection, using the default sort for this collection, which is usually al...
int Count
Gets the number of elements contained in the T:System.Collections.ICollection.
Definition: ICollection.cs:14
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
Defines a dictionary key/value pair that can be set or retrieved.
Represents a nongeneric collection of key/value pairs.
Definition: IDictionary.cs:8
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
bool Contains(PropertyDescriptor value)
Returns whether the collection contains the given T:System.ComponentModel.PropertyDescriptor.
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14
int IndexOf(PropertyDescriptor value)
Returns the index of the given T:System.ComponentModel.PropertyDescriptor.