AttributeCollection Class
Represents a collection of attributes.
Assembly: System (in System.dll)
Note: |
|---|
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: Synchronization. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes. |
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, Nothing 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 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 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
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: