Condividi tramite


AttributeCollection Classe

Definizione

Rappresenta una raccolta di attributi.

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
Ereditarietà
AttributeCollection
Attributi
Implementazioni

Esempio

Il primo esempio di codice verifica se l'oggetto BrowsableAttribute è stato impostato in questa raccolta. Il secondo esempio di codice ottiene il valore effettivo di DescriptionAttribute per un pulsante. Entrambi gli esempi richiedono che button1 e textBox1 siano stati creati in un modulo. Quando si usano attributi, verificare che sia stato impostato un attributo o accedervi.

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

Commenti

La AttributeCollection classe è di sola lettura e non implementa metodi per aggiungere o rimuovere attributi. Per implementare questi metodi, è necessario ereditare da questa classe.

Utilizzare la Count proprietà per trovare il numero di attributi presenti nell'insieme.

È anche possibile utilizzare i metodi di questa classe per eseguire una query sulla raccolta relativa al relativo contenuto. Chiamare il Contains metodo per verificare che nella raccolta esista un attributo o una matrice di attributi specificata. Chiamare il Matches metodo per verificare che nell'insieme esista un attributo o una matrice di attributi specificati e che i valori degli attributi specificati corrispondano ai valori dell'insieme.

Anche se la maggior parte degli attributi ha valori predefiniti, i valori predefiniti non sono obbligatori. Se un attributo non ha alcun valore predefinito, null viene restituito dalla proprietà indicizzata che accetta un tipo. Quando si definiscono attributi personalizzati, è possibile dichiarare un valore predefinito fornendo un costruttore che non accetta argomenti o definendo un campo statico pubblico del tipo di attributo denominato "Default".

Costruttori

Nome Descrizione
AttributeCollection()

Inizializza una nuova istanza della classe AttributeCollection.

AttributeCollection(Attribute[])

Inizializza una nuova istanza della classe AttributeCollection.

Campi

Nome Descrizione
Empty

Specifica una raccolta vuota che è possibile usare, anziché crearne una nuova. Il campo è di sola lettura.

Proprietà

Nome Descrizione
Attributes

Ottiene la raccolta di attributi.

Count

Ottiene il numero di attributi.

Item[Int32]

Ottiene l'attributo con il numero di indice specificato.

Item[Type]

Ottiene l'attributo con il tipo specificato.

Metodi

Nome Descrizione
Contains(Attribute)

Determina se questa raccolta di attributi ha l'attributo specificato.

Contains(Attribute[])

Determina se questa raccolta di attributi contiene tutti gli attributi specificati nella matrice di attributi.

CopyTo(Array, Int32)

Copia l'insieme in una matrice, a partire dall'indice specificato.

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
FromExisting(AttributeCollection, Attribute[])

Crea un nuovo AttributeCollection oggetto da un oggetto esistente AttributeCollection.

GetDefaultAttribute(Type)

Restituisce l'impostazione predefinita Attribute di un oggetto specificato Type.

GetEnumerator()

Ottiene un enumeratore per questa raccolta.

GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene il Type dell'istanza corrente.

(Ereditato da Object)
Matches(Attribute)

Determina se un attributo specificato è uguale a un attributo nella raccolta.

Matches(Attribute[])

Determina se gli attributi nella matrice specificata sono uguali agli attributi nella raccolta.

MemberwiseClone()

Crea una copia superficiale del Objectcorrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Implementazioni dell'interfaccia esplicita

Nome Descrizione
ICollection.Count

Ottiene il numero di elementi contenuti nell'insieme.

ICollection.IsSynchronized

Ottiene un valore che indica se l'accesso alla raccolta è sincronizzato (thread-safe).

ICollection.SyncRoot

Ottiene un oggetto che può essere utilizzato per sincronizzare l'accesso all'insieme.

IEnumerable.GetEnumerator()

Restituisce un oggetto IEnumerator per l'oggetto IDictionary.

Metodi di estensione

Nome Descrizione
AsParallel(IEnumerable)

Abilita la parallelizzazione di una query.

AsQueryable(IEnumerable)

Converte un IEnumerable in un IQueryable.

Cast<TResult>(IEnumerable)

Esegue il cast degli elementi di un IEnumerable al tipo specificato.

OfType<TResult>(IEnumerable)

Filtra gli elementi di un IEnumerable in base a un tipo specificato.

Si applica a

Vedi anche