AttributeCollection AttributeCollection AttributeCollection AttributeCollection Class

Definition

Represents a collection of attributes.

public ref class AttributeCollection : System::Collections::ICollection
[System.Runtime.InteropServices.ComVisible(true)]
public class AttributeCollection : System.Collections.ICollection
type AttributeCollection = class
    interface ICollection
    interface IEnumerable
Public Class AttributeCollection
Implements ICollection
Inheritance
AttributeCollectionAttributeCollectionAttributeCollectionAttributeCollection
Attributes
Implements

Examples

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 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 Sub ContainsAttribute()
    ' Creates a new collection and assigns it the attributes for button1.
    Dim attributes As AttributeCollection
    attributes = TypeDescriptor.GetAttributes(button1)
    
    ' Sets an Attribute to the specific attribute.
    Dim myAttribute As BrowsableAttribute = BrowsableAttribute.Yes
    
    If attributes.Contains(myAttribute) Then
        textBox1.Text = "button1 has a browsable attribute."
    Else
        textBox1.Text = "button1 does not have a browsable attribute."
    End If
End Sub 'ContainsAttribute
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[DesignerAttribute::typeid]);
      
      // Prints the value of the attribute in a text box.
      textBox1->Text = myDesigner->DesignerTypeName;
   }
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;
 }
Private Sub GetAttributeValue()
    ' Creates a new collection and assigns it the attributes for button1.
    Dim attributes As AttributeCollection
    attributes = TypeDescriptor.GetAttributes(button1)
    
    ' Gets the designer attribute from the collection.
    Dim myDesigner As DesignerAttribute
    myDesigner = CType(attributes(GetType(DesignerAttribute)), DesignerAttribute)
    
    ' Prints the value of the attribute in a text box.
    textBox1.Text = myDesigner.DesignerTypeName
End Sub 'GetAttributeValue

Remarks

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".

Constructors

AttributeCollection() AttributeCollection() AttributeCollection() AttributeCollection()

Initializes a new instance of the AttributeCollection class.

AttributeCollection(Attribute[]) AttributeCollection(Attribute[]) AttributeCollection(Attribute[]) AttributeCollection(Attribute[])

Initializes a new instance of the AttributeCollection class.

Fields

Empty Empty Empty Empty

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

Properties

Attributes Attributes Attributes Attributes

Gets the attribute collection.

Count Count Count Count

Gets the number of attributes.

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

Gets the attribute with the specified index number.

Item[Type] Item[Type] Item[Type] Item[Type]

Gets the attribute with the specified type.

Methods

Contains(Attribute) Contains(Attribute) Contains(Attribute) Contains(Attribute)

Determines whether this collection of attributes has the specified attribute.

Contains(Attribute[]) Contains(Attribute[]) Contains(Attribute[]) Contains(Attribute[])

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

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

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

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
FromExisting(AttributeCollection, Attribute[]) FromExisting(AttributeCollection, Attribute[]) FromExisting(AttributeCollection, Attribute[]) FromExisting(AttributeCollection, Attribute[])

Creates a new AttributeCollection from an existing AttributeCollection.

GetDefaultAttribute(Type) GetDefaultAttribute(Type) GetDefaultAttribute(Type) GetDefaultAttribute(Type)

Returns the default Attribute of a given Type.

GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

Gets an enumerator for this collection.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
Matches(Attribute) Matches(Attribute) Matches(Attribute) Matches(Attribute)

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

Matches(Attribute[]) Matches(Attribute[]) Matches(Attribute[]) Matches(Attribute[])

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

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

ICollection.Count ICollection.Count ICollection.Count ICollection.Count

Gets the number of elements contained in the collection.

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

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

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

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

IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator()

Returns an IEnumerator for the IDictionary.

Extension Methods

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Applies to

See Also