AttributeCollection Class (System.ComponentModel)

Switch View :
ScriptFree
.NET Framework Class Library
AttributeCollection Class

Represents a collection of attributes.

Inheritance Hierarchy

System.Object
  System.ComponentModel.AttributeCollection

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

Visual Basic
<ComVisibleAttribute(True)> _
<HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization := True)> _
Public Class AttributeCollection _
	Implements ICollection, IEnumerable
C#
[ComVisibleAttribute(true)]
[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true)]
public class AttributeCollection : ICollection, 
	IEnumerable
Visual C++
[ComVisibleAttribute(true)]
[HostProtectionAttribute(SecurityAction::LinkDemand, Synchronization = true)]
public ref class AttributeCollection : ICollection, 
	IEnumerable
F#
[<ComVisibleAttribute(true)>]
[<HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true)>]
type AttributeCollection =  
    class
        interface ICollection
        interface IEnumerable
    end

The AttributeCollection type exposes the following members.

Constructors

  Name Description
Protected method AttributeCollection() Initializes a new instance of the AttributeCollection class.
Public method Supported by the XNA Framework AttributeCollection(Attribute[]) Initializes a new instance of the AttributeCollection class.
Top
Properties

  Name Description
Protected property Attributes Gets the attribute collection.
Public property Supported by the XNA Framework Count Gets the number of attributes.
Public property Supported by the XNA Framework Item[Int32] Gets the attribute with the specified index number.
Public property Supported by the XNA Framework Item[Type] Gets the attribute with the specified type.
Top
Methods

  Name Description
Public method Supported by the XNA Framework Contains(Attribute) Determines whether this collection of attributes has the specified attribute.
Public method Contains(Attribute[]) Determines whether this attribute collection contains all the specified attributes in the attribute array.
Public method Supported by the XNA Framework CopyTo Copies the collection to an array, starting at the specified index.
Public method Supported by the XNA Framework Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Supported by the XNA Framework Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Static member FromExisting Creates a new AttributeCollection from an existing AttributeCollection.
Protected method Supported by the XNA Framework GetDefaultAttribute Returns the default Attribute of a given Type.
Public method Supported by the XNA Framework GetEnumerator Gets an enumerator for this collection.
Public method Supported by the XNA Framework GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method Supported by the XNA Framework GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Supported by the XNA Framework Matches(Attribute) Determines whether a specified attribute is the same as an attribute in the collection.
Public method Matches(Attribute[]) Determines whether the attributes in the specified array are the same as the attributes in the collection.
Protected method Supported by the XNA Framework MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Supported by the XNA Framework ToString Returns a string that represents the current object. (Inherited from Object.)
Top
Extension Methods

  Name Description
Public Extension Method AsParallel Enables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension Method AsQueryable Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension Method Supported by the XNA Framework Cast<TResult> Converts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension Method Supported by the XNA Framework OfType<TResult> Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top
Fields

  Name Description
Public field Static member Supported by the XNA Framework Empty Specifies an empty collection that you can use, rather than creating a new one. This field is read-only.
Top
Explicit Interface Implementations

  Name Description
Explicit interface implemetation Private property Supported by the XNA Framework ICollection.Count Gets the number of elements contained in the collection.
Explicit interface implemetation Private property Supported by the XNA Framework ICollection.IsSynchronized Gets a value indicating whether access to the collection is synchronized (thread-safe).
Explicit interface implemetation Private property Supported by the XNA Framework ICollection.SyncRoot Gets an object that can be used to synchronize access to the collection.
Explicit interface implemetation Private method Supported by the XNA Framework IEnumerable.GetEnumerator Returns an IEnumerator for the IDictionary.
Top
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".

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

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.

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


Visual 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.";
      }
   }


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;
 }


Visual 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[DesignerAttribute::typeid]);

      // Prints the value of the attribute in a text box.
      textBox1->Text = myDesigner->DesignerTypeName;
   }


Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
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.
See Also

Reference