AttributeCollection Clase

Definición

Representa una colección de atributos.

public ref class AttributeCollection : System::Collections::ICollection
public class AttributeCollection : System.Collections.ICollection
[System.Runtime.InteropServices.ComVisible(true)]
public class AttributeCollection : System.Collections.ICollection
type AttributeCollection = class
    interface ICollection
    interface IEnumerable
[<System.Runtime.InteropServices.ComVisible(true)>]
type AttributeCollection = class
    interface ICollection
    interface IEnumerable
Public Class AttributeCollection
Implements ICollection
Herencia
AttributeCollection
Atributos
Implementaciones

Ejemplos

El primer ejemplo de código comprueba si BrowsableAttribute se ha establecido en esta colección. El segundo ejemplo de código obtiene el valor real de DescriptionAttribute para un botón. Ambos ejemplos requieren que button1 y textBox1 se hayan creado en un formulario. Al usar atributos, compruebe que se ha establecido un atributo o acceda a su valor.

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.Text = "button1 has a browsable attribute.";
    else
       textBox1.Text = "button1 does not have a browsable attribute.";
 }
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
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[typeof(DesignerAttribute)];

    // Prints the value of the attribute in a text box.
    textBox1.Text = myDesigner.DesignerTypeName;
 }
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

Comentarios

La AttributeCollection clase es de solo lectura; no implementa métodos para agregar ni quitar atributos. Debe heredar de esta clase para implementar estos métodos.

Utilice la Count propiedad para buscar el número de atributos que existen en la colección.

También puede usar los métodos de esta clase para consultar la colección sobre su contenido. Llame al Contains método para comprobar que existe un atributo o matriz de atributos especificado en la colección. Llame al Matches método para comprobar que existe un atributo especificado o una matriz de atributos en la colección y que los valores de los atributos especificados son los mismos que los valores de la colección.

Aunque la mayoría de los atributos tienen valores predeterminados, no se requieren valores predeterminados. Si un atributo no tiene ningún valor predeterminado, null se devuelve de la propiedad indizada que toma un tipo. Al definir sus propios atributos, puede declarar un valor predeterminado proporcionando un constructor que no toma ningún argumento o definiendo un campo estático público del tipo de atributo denominado "Default".

Constructores

AttributeCollection()

Inicializa una nueva instancia de la clase AttributeCollection.

AttributeCollection(Attribute[])

Inicializa una nueva instancia de la clase AttributeCollection.

Campos

Empty

Especifica una colección vacía que se puede usar en lugar de crear una nueva. Este campo es de solo lectura.

Propiedades

Attributes

Obtiene la colección de atributos.

Count

Obtiene el número de atributos.

Item[Int32]

Obtiene el atributo que tiene el número de índice especificado.

Item[Type]

Obtiene el atributo que tiene el tipo especificado.

Métodos

Contains(Attribute)

Determina si esta colección de atributos tiene el atributo especificado.

Contains(Attribute[])

Determina si esta colección de atributos contiene todos los atributos especificados en la matriz de atributos.

CopyTo(Array, Int32)

Copia la colección en una matriz, empezando por el índice especificado.

Equals(Object)

Determina si el objeto especificado es igual que el objeto actual.

(Heredado de Object)
FromExisting(AttributeCollection, Attribute[])

Crea una nueva clase AttributeCollection a partir de una clase AttributeCollection existente.

GetDefaultAttribute(Type)

Devuelve el Attribute predeterminado de un tipo (Type) dado.

GetEnumerator()

Obtiene un enumerador para esta colección.

GetHashCode()

Sirve como la función hash predeterminada.

(Heredado de Object)
GetType()

Obtiene el Type de la instancia actual.

(Heredado de Object)
Matches(Attribute)

Determina si un atributo especificado es igual a un atributo de la colección.

Matches(Attribute[])

Determina si los atributos en la matriz especificada son iguales a los de la colección.

MemberwiseClone()

Crea una copia superficial del Object actual.

(Heredado de Object)
ToString()

Devuelve una cadena que representa el objeto actual.

(Heredado de Object)

Implementaciones de interfaz explícitas

ICollection.Count

Obtiene el número de elementos contenido en la colección.

ICollection.IsSynchronized

Obtiene un valor que indica si el acceso a la colección está sincronizado (es seguro para la ejecución de subprocesos).

ICollection.SyncRoot

Obtiene un objeto que se puede utilizar para sincronizar el acceso a la colección.

IEnumerable.GetEnumerator()

Devuelve una interfaz IEnumerator para la interfaz IDictionary.

Métodos de extensión

Cast<TResult>(IEnumerable)

Convierte los elementos de IEnumerable en el tipo especificado.

OfType<TResult>(IEnumerable)

Filtra los elementos de IEnumerable en función de un tipo especificado.

AsParallel(IEnumerable)

Habilita la paralelización de una consulta.

AsQueryable(IEnumerable)

Convierte una interfaz IEnumerable en IQueryable.

Se aplica a

Consulte también