/
PropertyDescriptorCollection.cs
627 lines (535 loc) · 19.3 KB
/
PropertyDescriptorCollection.cs
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace System.ComponentModel
{
/// <summary>
/// Represents a collection of properties.
/// </summary>
public class PropertyDescriptorCollection : ICollection, IList, IDictionary
{
/// <summary>
/// An empty PropertyDescriptorCollection that can used instead of creating a new one with no items.
/// </summary>
public static readonly PropertyDescriptorCollection Empty = new PropertyDescriptorCollection(null, true);
private IDictionary? _cachedFoundProperties;
private bool _cachedIgnoreCase;
private PropertyDescriptor[] _properties;
private readonly string[]? _namedSort;
private readonly IComparer? _comparer;
private bool _propsOwned;
private bool _needSort;
private readonly bool _readOnly;
private readonly object _internalSyncObject = new object();
/// <summary>
/// Initializes a new instance of the <see cref='System.ComponentModel.PropertyDescriptorCollection'/>
/// class.
/// </summary>
public PropertyDescriptorCollection(PropertyDescriptor[]? properties)
{
if (properties == null)
{
_properties = Array.Empty<PropertyDescriptor>();
Count = 0;
}
else
{
_properties = properties;
Count = properties.Length;
}
_propsOwned = true;
}
/// <summary>
/// Initializes a new instance of a property descriptor collection, and allows you to mark the
/// collection as read-only so it cannot be modified.
/// </summary>
public PropertyDescriptorCollection(PropertyDescriptor[]? properties, bool readOnly) : this(properties)
{
_readOnly = readOnly;
}
private PropertyDescriptorCollection(PropertyDescriptor[] properties, int propCount, string[]? namedSort, IComparer? comparer)
{
_propsOwned = false;
if (namedSort != null)
{
_namedSort = (string[])namedSort.Clone();
}
_comparer = comparer;
_properties = properties;
Count = propCount;
_needSort = true;
}
/// <summary>
/// Gets the number of property descriptors in the collection.
/// </summary>
public int Count { get; private set; }
/// <summary>
/// Gets the property with the specified index number.
/// </summary>
public virtual PropertyDescriptor this[int index]
{
get
{
if (index >= Count)
{
throw new IndexOutOfRangeException();
}
EnsurePropsOwned();
return _properties[index]!;
}
}
/// <summary>
/// Gets the property with the specified name.
/// </summary>
public virtual PropertyDescriptor? this[string name] => Find(name, false);
public int Add(PropertyDescriptor value)
{
if (_readOnly)
{
throw new NotSupportedException();
}
EnsureSize(Count + 1);
_properties[Count++] = value;
return Count - 1;
}
public void Clear()
{
if (_readOnly)
{
throw new NotSupportedException();
}
Count = 0;
_cachedFoundProperties = null;
}
public bool Contains(PropertyDescriptor value) => IndexOf(value) >= 0;
public void CopyTo(Array array, int index)
{
EnsurePropsOwned();
Array.Copy(_properties, 0, array, index, Count);
}
private void EnsurePropsOwned()
{
if (!_propsOwned)
{
_propsOwned = true;
if (_properties != null)
{
PropertyDescriptor[] newProps = new PropertyDescriptor[Count];
Array.Copy(_properties, newProps, Count);
_properties = newProps;
}
}
if (_needSort)
{
_needSort = false;
InternalSort(_namedSort);
}
}
private void EnsureSize(int sizeNeeded)
{
if (sizeNeeded <= _properties.Length)
{
return;
}
if (_properties.Length == 0)
{
Count = 0;
_properties = new PropertyDescriptor[sizeNeeded];
return;
}
EnsurePropsOwned();
int newSize = Math.Max(sizeNeeded, _properties.Length * 2);
PropertyDescriptor[] newProps = new PropertyDescriptor[newSize];
Array.Copy(_properties, newProps, Count);
_properties = newProps;
}
/// <summary>
/// Gets the description of the property with the specified name.
/// </summary>
public virtual PropertyDescriptor? Find(string name, bool ignoreCase)
{
lock (_internalSyncObject)
{
PropertyDescriptor? p = null;
if (_cachedFoundProperties == null || _cachedIgnoreCase != ignoreCase)
{
_cachedIgnoreCase = ignoreCase;
if (ignoreCase)
{
_cachedFoundProperties = new Hashtable(StringComparer.OrdinalIgnoreCase);
}
else
{
_cachedFoundProperties = new Hashtable();
}
}
// first try to find it in the cache
//
object? cached = _cachedFoundProperties[name];
if (cached != null)
{
return (PropertyDescriptor)cached;
}
// Now start walking from where we last left off, filling
// the cache as we go.
//
for (int i = 0; i < Count; i++)
{
if (ignoreCase)
{
if (string.Equals(_properties[i].Name, name, StringComparison.OrdinalIgnoreCase))
{
_cachedFoundProperties[name] = _properties[i];
p = _properties[i];
break;
}
}
else
{
if (_properties[i].Name.Equals(name))
{
_cachedFoundProperties[name] = _properties[i];
p = _properties[i];
break;
}
}
}
return p;
}
}
public int IndexOf(PropertyDescriptor? value) => Array.IndexOf(_properties, value, 0, Count);
public void Insert(int index, PropertyDescriptor value)
{
if (_readOnly)
{
throw new NotSupportedException();
}
EnsureSize(Count + 1);
if (index < Count)
{
Array.Copy(_properties, index, _properties, index + 1, Count - index);
}
_properties[index] = value;
Count++;
}
public void Remove(PropertyDescriptor? value)
{
if (_readOnly)
{
throw new NotSupportedException();
}
int index = IndexOf(value);
if (index != -1)
{
RemoveAt(index);
}
}
public void RemoveAt(int index)
{
if (_readOnly)
{
throw new NotSupportedException();
}
if (index < Count - 1)
{
Array.Copy(_properties, index + 1, _properties, index, Count - index - 1);
}
_properties[Count - 1] = null!;
Count--;
}
/// <summary>
/// Sorts the members of this PropertyDescriptorCollection, using the default sort for this collection,
/// which is usually alphabetical.
/// </summary>
public virtual PropertyDescriptorCollection Sort()
{
return new PropertyDescriptorCollection(_properties, Count, _namedSort, _comparer);
}
/// <summary>
/// Sorts the members of this PropertyDescriptorCollection. Any specified NamedSort arguments will
/// be applied first, followed by sort using the specified IComparer.
/// </summary>
public virtual PropertyDescriptorCollection Sort(string[]? names)
{
return new PropertyDescriptorCollection(_properties, Count, names, _comparer);
}
/// <summary>
/// Sorts the members of this PropertyDescriptorCollection. Any specified NamedSort arguments will
/// be applied first, followed by sort using the specified IComparer.
/// </summary>
public virtual PropertyDescriptorCollection Sort(string[]? names, IComparer? comparer)
{
return new PropertyDescriptorCollection(_properties, Count, names, comparer);
}
/// <summary>
/// Sorts the members of this PropertyDescriptorCollection, using the specified IComparer to compare,
/// the PropertyDescriptors contained in the collection.
/// </summary>
public virtual PropertyDescriptorCollection Sort(IComparer? comparer)
{
return new PropertyDescriptorCollection(_properties, Count, _namedSort, comparer);
}
/// <summary>
/// Sorts the members of this PropertyDescriptorCollection. Any specified NamedSort arguments will
/// be applied first, followed by sort using the specified IComparer.
/// </summary>
protected void InternalSort(string[]? names)
{
if (_properties.Length == 0)
{
return;
}
InternalSort(_comparer);
if (names != null && names.Length > 0)
{
List<PropertyDescriptor?> propList = new List<PropertyDescriptor?>(_properties);
int foundCount = 0;
int propCount = _properties.Length;
for (int i = 0; i < names.Length; i++)
{
for (int j = 0; j < propCount; j++)
{
PropertyDescriptor? currentProp = propList[j];
// Found a matching property. Here, we add it to our array. We also
// mark it as null in our array list so we don't add it twice later.
//
if (currentProp != null && currentProp.Name.Equals(names[i]))
{
_properties[foundCount++] = currentProp;
propList[j] = null;
break;
}
}
}
// At this point we have filled in the first "foundCount" number of propeties, one for each
// name in our name array. If a name didn't match, then it is ignored. Next, we must fill
// in the rest of the properties. We now have a sparse array containing the remainder, so
// it's easy.
//
for (int i = 0; i < propCount; i++)
{
if (propList[i] != null)
{
_properties[foundCount++] = propList[i]!;
}
}
Debug.Assert(foundCount == propCount, "We did not completely fill our property array");
}
}
/// <summary>
/// Sorts the members of this PropertyDescriptorCollection using the specified IComparer.
/// </summary>
protected void InternalSort(IComparer? sorter)
{
if (sorter == null)
{
TypeDescriptor.SortDescriptorArray(this);
}
else
{
Array.Sort(_properties, sorter);
}
}
/// <summary>
/// Gets an enumerator for this <see cref='System.ComponentModel.PropertyDescriptorCollection'/>.
/// </summary>
public virtual IEnumerator GetEnumerator()
{
EnsurePropsOwned();
// we can only return an enumerator on the props we actually have...
if (_properties.Length != Count)
{
PropertyDescriptor[] enumProps = new PropertyDescriptor[Count];
Array.Copy(_properties, enumProps, Count);
return enumProps.GetEnumerator();
}
return _properties.GetEnumerator();
}
bool ICollection.IsSynchronized => false;
object ICollection.SyncRoot => null!;
int ICollection.Count => Count;
void IList.Clear() => Clear();
void IDictionary.Clear() => Clear();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
void IList.RemoveAt(int index) => RemoveAt(index);
void IDictionary.Add(object key, object? value)
{
if (!(value is PropertyDescriptor newProp))
{
throw new ArgumentException(nameof(value));
}
Add(newProp);
}
bool IDictionary.Contains(object key)
{
if (key is string)
{
return this[(string)key] != null;
}
return false;
}
IDictionaryEnumerator IDictionary.GetEnumerator() => new PropertyDescriptorEnumerator(this);
bool IDictionary.IsFixedSize => _readOnly;
bool IDictionary.IsReadOnly => _readOnly;
object? IDictionary.this[object key]
{
get
{
if (key is string)
{
return this[(string)key];
}
return null;
}
set
{
if (_readOnly)
{
throw new NotSupportedException();
}
if (value != null && !(value is PropertyDescriptor))
{
throw new ArgumentException(nameof(value));
}
int index = -1;
if (key is int)
{
index = (int)key;
if (index < 0 || index >= Count)
{
throw new IndexOutOfRangeException();
}
}
else if (key is string)
{
for (int i = 0; i < Count; i++)
{
if (_properties[i]!.Name.Equals((string)key))
{
index = i;
break;
}
}
}
else
{
throw new ArgumentException(nameof(key));
}
if (index == -1)
{
Add((PropertyDescriptor)value!);
}
else
{
EnsurePropsOwned();
_properties[index] = (PropertyDescriptor)value!;
if (_cachedFoundProperties != null && key is string)
{
_cachedFoundProperties[key] = value;
}
}
}
}
ICollection IDictionary.Keys
{
get
{
string[] keys = new string[Count];
for (int i = 0; i < Count; i++)
{
keys[i] = _properties[i]!.Name;
}
return keys;
}
}
ICollection IDictionary.Values
{
get
{
// We can only return an enumerator on the props we actually have.
if (_properties.Length != Count)
{
PropertyDescriptor[] newProps = new PropertyDescriptor[Count];
Array.Copy(_properties, newProps, Count);
return newProps;
}
else
{
return (ICollection)_properties.Clone();
}
}
}
void IDictionary.Remove(object key)
{
if (key is string)
{
PropertyDescriptor? pd = this[(string)key];
if (pd != null)
{
((IList)this).Remove(pd);
}
}
}
int IList.Add(object? value) => Add((PropertyDescriptor)value!);
bool IList.Contains(object? value) => Contains((PropertyDescriptor)value!);
int IList.IndexOf(object? value) => IndexOf((PropertyDescriptor)value!);
void IList.Insert(int index, object? value) => Insert(index, (PropertyDescriptor)value!);
bool IList.IsReadOnly => _readOnly;
bool IList.IsFixedSize => _readOnly;
void IList.Remove(object? value) => Remove((PropertyDescriptor?)value);
object? IList.this[int index]
{
get => this[index];
set
{
if (_readOnly)
{
throw new NotSupportedException();
}
if (index >= Count)
{
throw new IndexOutOfRangeException();
}
if (value != null && !(value is PropertyDescriptor))
{
throw new ArgumentException(nameof(value));
}
EnsurePropsOwned();
_properties[index] = (PropertyDescriptor)value!;
}
}
private sealed class PropertyDescriptorEnumerator : IDictionaryEnumerator
{
private readonly PropertyDescriptorCollection _owner;
private int _index = -1;
public PropertyDescriptorEnumerator(PropertyDescriptorCollection owner)
{
_owner = owner;
}
public object Current => Entry;
public DictionaryEntry Entry
{
get
{
PropertyDescriptor curProp = _owner[_index];
return new DictionaryEntry(curProp.Name, curProp);
}
}
public object Key => _owner[_index].Name;
public object Value => _owner[_index].Name;
public bool MoveNext()
{
if (_index < (_owner.Count - 1))
{
_index++;
return true;
}
return false;
}
public void Reset() => _index = -1;
}
}
}