AttributeCollection Class
TOC
Collapse the table of content
Expand the table of content

AttributeCollection Class

 

Represents a collection of attributes.

Namespace:   System.ComponentModel
Assembly:  System (in System.dll)

System.Object
  System.ComponentModel.AttributeCollection

[ComVisibleAttribute(true)]
[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true)]
public class AttributeCollection : ICollection, IEnumerable

NameDescription
System_CAPS_protmethodAttributeCollection()

Initializes a new instance of the AttributeCollection class.

System_CAPS_pubmethodAttributeCollection(Attribute[])

Initializes a new instance of the AttributeCollection class.

NameDescription
System_CAPS_protpropertyAttributes

Gets the attribute collection.

System_CAPS_pubpropertyCount

Gets the number of attributes.

System_CAPS_pubpropertyItem[Int32]

Gets the attribute with the specified index number.

System_CAPS_pubpropertyItem[Type]

Gets the attribute with the specified type.

NameDescription
System_CAPS_pubmethodContains(Attribute)

Determines whether this collection of attributes has the specified attribute.

System_CAPS_pubmethodContains(Attribute[])

Determines whether this attribute collection contains all the specified attributes in the attribute array.

System_CAPS_pubmethodCopyTo(Array, Int32)

Copies the collection to an array, starting at the specified index.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticFromExisting(AttributeCollection, Attribute[])

Creates a new AttributeCollection from an existing AttributeCollection.

System_CAPS_protmethodGetDefaultAttribute(Type)

Returns the default Attribute of a given Type.

System_CAPS_pubmethodGetEnumerator()

Gets an enumerator for this collection.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodMatches(Attribute)

Determines whether a specified attribute is the same as an attribute in the collection.

System_CAPS_pubmethodMatches(Attribute[])

Determines whether the attributes in the specified array are the same as the attributes in the collection.

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubfieldSystem_CAPS_staticEmpty

Specifies an empty collection that you can use, rather than creating a new one. This field is read-only.

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodIEnumerable.GetEnumerator()

Returns an IEnumerator for the IDictionary.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.Count

Gets the number of elements contained in the collection.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.IsSynchronized

Gets a value indicating whether access to the collection is synchronized (thread-safe).

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.SyncRoot

Gets an object that can be used to synchronize access to the collection.

NameDescription
System_CAPS_pubmethodAsParallel()

Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)

System_CAPS_pubmethodAsQueryable()

Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)

System_CAPS_pubmethodCast<TResult>()

Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)

System_CAPS_pubmethodOfType<TResult>()

Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

The AttributeCollection class is read-only; it does not implement methods to add or remove attributes. You must inherit from this class to implement these methods.

Use the Count property to find the number of attributes that exist in the collection.

You can also use the methods of this class to query the collection about its contents. Call the Contains method to verify that a specified attribute or attribute array exists in the collection. Call the Matches method to verify that a specified attribute or array of attributes exists in the collection, and that the values of the specified attributes are the same as the values in the collection.

While most attributes have default values, default values are not required. If an attribute has no default value, null is returned from the indexed property that takes a type. When defining your own attributes, you can declare a default value by either providing a constructor that takes no arguments, or defining a public static field of your attribute type named "Default".

The first code example checks to see whether the BrowsableAttribute has been set in this collection. The second code example gets the actual value of the DescriptionAttribute for a button. Both examples require that button1 and textBox1 have been created on a form. When using attributes, verify that an attribute has been set, or access its value.

private void ContainsAttribute() {
    // Creates a new collection and assigns it the attributes for button1.
    AttributeCollection attributes;
    attributes = TypeDescriptor.GetAttributes(button1);

    // Sets an Attribute to the specific attribute.
    BrowsableAttribute myAttribute = BrowsableAttribute.Yes;

    if (attributes.Contains(myAttribute))
       textBox1.Text = "button1 has a browsable attribute.";
    else
       textBox1.Text = "button1 does not have a browsable attribute.";
 }
private void GetAttributeValue() {
    // Creates a new collection and assigns it the attributes for button1.
    AttributeCollection attributes;
    attributes = TypeDescriptor.GetAttributes(button1);

    // Gets the designer attribute from the collection.
    DesignerAttribute myDesigner; 
    myDesigner = (DesignerAttribute)attributes[typeof(DesignerAttribute)];

    // Prints the value of the attribute in a text box.
    textBox1.Text = myDesigner.DesignerTypeName;
 }

.NET Framework
Available since 1.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft