Export (0) Print
Expand All

AttributeCollection Class

Represents a collection of attributes.

For a list of all members of this type, see AttributeCollection Members.

System.Object
   System.ComponentModel.AttributeCollection

[Visual Basic]
<ComVisible(True)>
Public Class AttributeCollection
   Implements ICollection, IEnumerable
[C#]
[ComVisible(true)]
public class AttributeCollection : ICollection, IEnumerable
[C++]
[ComVisible(true)]
public __gc class AttributeCollection : public ICollection,
   IEnumerable
[JScript]
public
   ComVisible(true)
class AttributeCollection implements ICollection,
   IEnumerable

Thread Safety

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

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 Contains to verify that a specified attribute or attribute array exists in the collection. Call Matches 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, it is not required. If an attribute has no default value, a null reference (Nothing in Visual Basic) 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".

Example

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

[Visual Basic] 
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

[C#] 
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.";
 }

[C++] 
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 = S"button1 has a browsable attribute.";
    else
        textBox1->Text = S"button1 does not have a browsable attribute.";
}

[JScript] 
public function ContainsAttribute() {
    // Creates a new collection and assigns it the attributes for button1.
    var attributes : AttributeCollection;
    attributes = TypeDescriptor.GetAttributes(button1);
 
    // Sets an Attribute to the specific attribute.
    var myAttribute : BrowsableAttribute  = BrowsableAttribute.Yes;
 
    if (attributes.Contains(myAttribute))
       textBox1.Text = "button1 has a browsable attribute.";
    else
       textBox1.Text = "button1 does not have a browsable attribute.";
 }

[Visual Basic] 
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

[C#] 
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;
 }

[C++] 
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 = dynamic_cast<DesignerAttribute*>( attributes->get_Item( __typeof( DesignerAttribute )));
 
    // Prints the value of the attribute in a text box.
    textBox1->Text = myDesigner->DesignerTypeName;
}

[JScript] 
public function GetAttributeValue() {
    // Creates a new collection and assigns it the attributes for button1.
    var attributes : AttributeCollection ;
    attributes = TypeDescriptor.GetAttributes(button1);
 
    // Gets the designer attribute from the collection.
    var myDesigner : DesignerAttribute ; 
    myDesigner = DesignerAttribute(attributes[DesignerAttribute.GetType()]);
 
    // Prints the value of the attribute in a text box.
    if(myDesigner)
        textBox1.Text = myDesigner.DesignerTypeName;
 }

Requirements

Namespace: System.ComponentModel

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System (in System.dll)

See Also

AttributeCollection Members | System.ComponentModel Namespace | Attribute | BrowsableAttribute | DescriptionAttribute

Show:
© 2014 Microsoft