Compartir por


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 se ha establecido en BrowsableAttribute 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.";
      }
   }
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;

    textBox1.Text = attributes.Contains(myAttribute) ? "button1 has a browsable attribute." : "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;
   }
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 o matriz especificados 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

Nombre Description
AttributeCollection()

Inicializa una nueva instancia de la clase AttributeCollection.

AttributeCollection(Attribute[])

Inicializa una nueva instancia de la clase AttributeCollection.

Campos

Nombre Description
Empty

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

Propiedades

Nombre Description
Attributes

Obtiene la colección de atributos.

Count

Obtiene el número de atributos.

Item[Int32]

Obtiene el atributo con el número de índice especificado.

Item[Type]

Obtiene el atributo con el tipo especificado.

Métodos

Nombre Description
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 al objeto actual.

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

Crea un nuevo AttributeCollection a partir de un existente AttributeCollection.

GetDefaultAttribute(Type)

Devuelve el valor predeterminado Attribute de un determinado Type.

GetEnumerator()

Obtiene un enumerador para esta colección.

GetHashCode()

Actúa como 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 el mismo que un atributo de la colección.

Matches(Attribute[])

Determina si los atributos de la matriz especificada son los mismos que los atributos de la colección.

MemberwiseClone()

Crea una copia superficial del Objectactual.

(Heredado de Object)
ToString()

Devuelve una cadena que representa el objeto actual.

(Heredado de Object)

Implementaciones de interfaz explícitas

Nombre Description
ICollection.Count

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

ICollection.IsSynchronized

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

ICollection.SyncRoot

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

IEnumerable.GetEnumerator()

Devuelve un IEnumerator para .IDictionary

Métodos de extensión

Nombre Description
AsParallel(IEnumerable)

Habilita la paralelización de una consulta.

AsQueryable(IEnumerable)

Convierte un IEnumerable en un IQueryable.

Cast<TResult>(IEnumerable)

Convierte los elementos de un IEnumerable al tipo especificado.

OfType<TResult>(IEnumerable)

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

Se aplica a

Consulte también