AttributeCollection Class
Assembly: System (in system.dll)
'Declaration <ComVisibleAttribute(True)> _ Public Class AttributeCollection Implements ICollection, IEnumerable 'Usage Dim instance As AttributeCollection
/** @attribute ComVisibleAttribute(true) */ public class AttributeCollection implements ICollection, IEnumerable
ComVisibleAttribute(true) public class AttributeCollection implements ICollection, IEnumerable
Not applicable.
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, 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".
Note: |
|---|
| The HostProtectionAttribute attribute applied to this class 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 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 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.set_Text("button1 has a browsable attribute.");
}
else {
textBox1.set_Text("button1 does not have a browsable attribute.");
}
} //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
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.get_Item(DesignerAttribute.class.ToType())));
// Prints the value of the attribute in a text box.
textBox1.set_Text(myDesigner.get_DesignerTypeName());
} //GetAttributeValue
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; }
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
Note: