Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

AttributeCollection Class

Represents a collection of attributes.

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

[ComVisibleAttribute(true)] 
public ref class AttributeCollection : ICollection, IEnumerable
/** @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".

NoteNote:

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:
   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.set_Text("button1 has a browsable attribute.");
    }
    else {
        textBox1.set_Text("button1 does not have a browsable attribute.");
    }
} //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.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;
 }

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

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.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.