mscorlib(4.0.0.0) API with additions
AttributeCollection.cs
1 using System.Collections;
2 using System.Reflection;
5 
6 namespace System.ComponentModel
7 {
9  [ComVisible(true)]
10  [HostProtection(SecurityAction.LinkDemand, Synchronization = true)]
12  {
13  private struct AttributeEntry
14  {
15  public Type type;
16 
17  public int index;
18  }
19 
21  public static readonly AttributeCollection Empty = new AttributeCollection((Attribute[])null);
22 
23  private static Hashtable _defaultAttributes;
24 
25  private Attribute[] _attributes;
26 
27  private static object internalSyncObject = new object();
28 
29  private const int FOUND_TYPES_LIMIT = 5;
30 
31  private AttributeEntry[] _foundAttributeTypes;
32 
33  private int _index;
34 
37  protected virtual Attribute[] Attributes => _attributes;
38 
41  public int Count => Attributes.Length;
42 
46  public virtual Attribute this[int index]
47  {
48  get
49  {
50  return Attributes[index];
51  }
52  }
53 
57  public virtual Attribute this[Type attributeType]
58  {
59  get
60  {
61  lock (internalSyncObject)
62  {
63  if (_foundAttributeTypes == null)
64  {
65  _foundAttributeTypes = new AttributeEntry[5];
66  }
67  int i;
68  for (i = 0; i < 5; i++)
69  {
70  if (_foundAttributeTypes[i].type == attributeType)
71  {
72  int index = _foundAttributeTypes[i].index;
73  if (index != -1)
74  {
75  return Attributes[index];
76  }
77  return GetDefaultAttribute(attributeType);
78  }
79  if (_foundAttributeTypes[i].type == null)
80  {
81  break;
82  }
83  }
84  i = _index++;
85  if (_index >= 5)
86  {
87  _index = 0;
88  }
89  _foundAttributeTypes[i].type = attributeType;
90  int num = Attributes.Length;
91  for (int j = 0; j < num; j++)
92  {
93  Attribute attribute = Attributes[j];
94  Type type = attribute.GetType();
95  if (type == attributeType)
96  {
97  _foundAttributeTypes[i].index = j;
98  return attribute;
99  }
100  }
101  for (int k = 0; k < num; k++)
102  {
103  Attribute attribute2 = Attributes[k];
104  Type type2 = attribute2.GetType();
105  if (attributeType.IsAssignableFrom(type2))
106  {
107  _foundAttributeTypes[i].index = k;
108  return attribute2;
109  }
110  }
111  _foundAttributeTypes[i].index = -1;
112  return GetDefaultAttribute(attributeType);
113  }
114  }
115  }
116 
119  int ICollection.Count
120  {
121  get
122  {
123  return Count;
124  }
125  }
126 
131  {
132  get
133  {
134  return false;
135  }
136  }
137 
140  object ICollection.SyncRoot
141  {
142  get
143  {
144  return null;
145  }
146  }
147 
152  public AttributeCollection(params Attribute[] attributes)
153  {
154  if (attributes == null)
155  {
156  attributes = new Attribute[0];
157  }
158  _attributes = attributes;
159  int num = 0;
160  while (true)
161  {
162  if (num < attributes.Length)
163  {
164  if (attributes[num] == null)
165  {
166  break;
167  }
168  num++;
169  continue;
170  }
171  return;
172  }
173  throw new ArgumentNullException("attributes");
174  }
175 
178  {
179  }
180 
187  public static AttributeCollection FromExisting(AttributeCollection existing, params Attribute[] newAttributes)
188  {
189  if (existing == null)
190  {
191  throw new ArgumentNullException("existing");
192  }
193  if (newAttributes == null)
194  {
195  newAttributes = new Attribute[0];
196  }
197  Attribute[] array = new Attribute[existing.Count + newAttributes.Length];
198  int num = existing.Count;
199  existing.CopyTo(array, 0);
200  for (int i = 0; i < newAttributes.Length; i++)
201  {
202  if (newAttributes[i] == null)
203  {
204  throw new ArgumentNullException("newAttributes");
205  }
206  bool flag = false;
207  for (int j = 0; j < existing.Count; j++)
208  {
209  if (array[j].TypeId.Equals(newAttributes[i].TypeId))
210  {
211  flag = true;
212  array[j] = newAttributes[i];
213  break;
214  }
215  }
216  if (!flag)
217  {
218  array[num++] = newAttributes[i];
219  }
220  }
221  Attribute[] array2 = null;
222  if (num < array.Length)
223  {
224  array2 = new Attribute[num];
225  Array.Copy(array, 0, array2, 0, num);
226  }
227  else
228  {
229  array2 = array;
230  }
231  return new AttributeCollection(array2);
232  }
233 
238  public bool Contains(Attribute attribute)
239  {
240  Attribute attribute2 = this[attribute.GetType()];
241  if (attribute2 != null && attribute2.Equals(attribute))
242  {
243  return true;
244  }
245  return false;
246  }
247 
252  public bool Contains(Attribute[] attributes)
253  {
254  if (attributes == null)
255  {
256  return true;
257  }
258  for (int i = 0; i < attributes.Length; i++)
259  {
260  if (!Contains(attributes[i]))
261  {
262  return false;
263  }
264  }
265  return true;
266  }
267 
271  protected Attribute GetDefaultAttribute(Type attributeType)
272  {
273  lock (internalSyncObject)
274  {
275  if (_defaultAttributes == null)
276  {
277  _defaultAttributes = new Hashtable();
278  }
279  if (_defaultAttributes.ContainsKey(attributeType))
280  {
281  return (Attribute)_defaultAttributes[attributeType];
282  }
283  Attribute attribute = null;
284  Type reflectionType = TypeDescriptor.GetReflectionType(attributeType);
285  FieldInfo field = reflectionType.GetField("Default", BindingFlags.Static | BindingFlags.Public | BindingFlags.GetField);
286  if (field != null && field.IsStatic)
287  {
288  attribute = (Attribute)field.GetValue(null);
289  }
290  else
291  {
292  ConstructorInfo constructor = reflectionType.UnderlyingSystemType.GetConstructor(new Type[0]);
293  if (constructor != null)
294  {
295  attribute = (Attribute)constructor.Invoke(new object[0]);
296  if (!attribute.IsDefaultAttribute())
297  {
298  attribute = null;
299  }
300  }
301  }
302  _defaultAttributes[attributeType] = attribute;
303  return attribute;
304  }
305  }
306 
310  {
311  return Attributes.GetEnumerator();
312  }
313 
318  public bool Matches(Attribute attribute)
319  {
320  for (int i = 0; i < Attributes.Length; i++)
321  {
322  if (Attributes[i].Match(attribute))
323  {
324  return true;
325  }
326  }
327  return false;
328  }
329 
334  public bool Matches(Attribute[] attributes)
335  {
336  for (int i = 0; i < attributes.Length; i++)
337  {
338  if (!Matches(attributes[i]))
339  {
340  return false;
341  }
342  }
343  return true;
344  }
345 
349  public void CopyTo(Array array, int index)
350  {
351  Array.Copy(Attributes, 0, array, index, Attributes.Length);
352  }
353 
357  {
358  return GetEnumerator();
359  }
360  }
361 }
abstract FieldInfo GetField(string name, BindingFlags bindingAttr)
Searches for the specified field, using the specified binding constraints.
int Count
Gets the number of attributes.
object SyncRoot
Gets an object that can be used to synchronize access to the T:System.Collections....
Definition: ICollection.cs:23
static readonly AttributeCollection Empty
Specifies an empty collection that you can use, rather than creating a new one. This field is read-on...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
bool IsStatic
Gets a value indicating whether the field is static.
Definition: FieldInfo.cs:131
bool Matches(Attribute attribute)
Determines whether a specified attribute is the same as an attribute in the collection.
Discovers the attributes of a class constructor and provides access to constructor metadata.
Represents the base class for custom attributes.
Definition: Attribute.cs:15
Discovers the attributes of a field and provides access to field metadata.
Definition: FieldInfo.cs:15
BindingFlags
Specifies flags that control binding and the way in which the search for members and types is conduct...
Definition: BindingFlags.cs:10
Definition: __Canon.cs:3
abstract Type UnderlyingSystemType
Indicates the type provided by the common language runtime that represents this type.
Definition: Type.cs:816
ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
Searches for a constructor whose parameters match the specified argument types and modifiers,...
Definition: Type.cs:1378
abstract object GetValue(object obj)
When overridden in a derived class, returns the value of a field supported by a given object.
bool Contains(Attribute attribute)
Determines whether this collection of attributes has the specified attribute.
virtual bool ContainsKey(object key)
Determines whether the T:System.Collections.Hashtable contains a specific key.
Definition: Hashtable.cs:983
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.
Attribute GetDefaultAttribute(Type attributeType)
Returns the default T:System.Attribute of a given T:System.Type.
AttributeCollection(params Attribute[] attributes)
Initializes a new instance of the T:System.ComponentModel.AttributeCollection class.
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
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
Provides information about the characteristics for a component, such as its attributes,...
bool Contains(Attribute[] attributes)
Determines whether this attribute collection contains all the specified attributes in the attribute a...
IEnumerator GetEnumerator()
Gets an enumerator for this collection.
AttributeCollection()
Initializes a new instance of the T:System.ComponentModel.AttributeCollection class.
IEnumerator GetEnumerator()
Returns an enumerator that iterates through a collection.
Represents a collection of attributes.
void CopyTo(Array array, int index)
Copies the collection to an array, starting at the specified index.
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
static Type GetReflectionType(Type type)
Returns a T:System.Type that can be used to perform reflection, given a class type.
abstract object Invoke(BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
When implemented in a derived class, invokes the constructor reflected by this ConstructorInfo with t...
virtual Attribute [] Attributes
Gets the attribute collection.
bool Matches(Attribute[] attributes)
Determines whether the attributes in the specified array are the same as the attributes in the collec...
static AttributeCollection FromExisting(AttributeCollection existing, params Attribute[] newAttributes)
Creates a new T:System.ComponentModel.AttributeCollection from an existing T:System....
int Count
Gets the number of elements contained in the T:System.Collections.ICollection.
Definition: ICollection.cs:14
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
override bool Equals(object obj)
Returns a value that indicates whether this instance is equal to a specified object.
Definition: Attribute.cs:1026
virtual bool IsDefaultAttribute()
When overridden in a derived class, indicates whether the value of this instance is the default value...
Definition: Attribute.cs:1124
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9