AttributeCollection Classe

Définition

Représente une collection d’attributs.

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
Héritage
AttributeCollection
Attributs
Implémente

Exemples

Le premier exemple de code vérifie si a BrowsableAttribute été défini dans cette collection. Le deuxième exemple de code obtient la valeur réelle de pour DescriptionAttribute un bouton. Les deux exemples nécessitent que button1 et textBox1 ont été créés sur un formulaire. Lorsque vous utilisez des attributs, vérifiez qu’un attribut a été défini ou accédez à sa valeur.

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

Remarques

La AttributeCollection classe est en lecture seule ; elle n’implémente pas de méthodes pour ajouter ou supprimer des attributs. Vous devez hériter de cette classe pour implémenter ces méthodes.

Utilisez la Count propriété pour rechercher le nombre d’attributs qui existent dans la collection.

Vous pouvez également utiliser les méthodes de cette classe pour interroger la collection sur son contenu. Appelez la Contains méthode pour vérifier qu’un attribut ou un tableau d’attributs spécifié existe dans la collection. Appelez la Matches méthode pour vérifier qu’un attribut ou un tableau d’attributs spécifié existe dans la collection et que les valeurs des attributs spécifiés sont identiques aux valeurs de la collection.

Bien que la plupart des attributs aient des valeurs par défaut, les valeurs par défaut ne sont pas obligatoires. Si un attribut n’a pas de valeur par défaut, null est retourné à partir de la propriété indexée qui accepte un type. Lorsque vous définissez vos propres attributs, vous pouvez déclarer une valeur par défaut en fournissant un constructeur qui ne prend aucun argument, ou en définissant un champ statique public de votre type d’attribut nommé « Default ».

Constructeurs

AttributeCollection()

Initialise une nouvelle instance de la classe AttributeCollection.

AttributeCollection(Attribute[])

Initialise une nouvelle instance de la classe AttributeCollection.

Champs

Empty

Spécifie une collection vide que vous pouvez utiliser, plutôt que d'en créer une nouvelle. Ce champ est en lecture seule.

Propriétés

Attributes

Obtient la collection d'attributs.

Count

Obtient le nombre d'attributs.

Item[Int32]

Obtient l'attribut avec le numéro d'index spécifié.

Item[Type]

Obtient l'attribut du type spécifié.

Méthodes

Contains(Attribute)

Détermine si cette collection d'attributs possède l'attribut spécifié.

Contains(Attribute[])

Détermine si cette collection d'attributs contient tous les attributs spécifiés dans le tableau d'attributs.

CopyTo(Array, Int32)

Copie la collection dans un tableau, en commençant à l'index spécifié.

Equals(Object)

Détermine si l'objet spécifié est égal à l'objet actuel.

(Hérité de Object)
FromExisting(AttributeCollection, Attribute[])

Crée un nouveau AttributeCollection à partir d'un AttributeCollection existant.

GetDefaultAttribute(Type)

Retourne le Attribute par défaut d'un Type donné.

GetEnumerator()

Obtient un énumérateur pour cette collection.

GetHashCode()

Fait office de fonction de hachage par défaut.

(Hérité de Object)
GetType()

Obtient le Type de l'instance actuelle.

(Hérité de Object)
Matches(Attribute)

Détermine si un attribut spécifié est identique à un attribut de la collection.

Matches(Attribute[])

Détermine si les attributs du tableau spécifié sont identiques aux attributs de la collection.

MemberwiseClone()

Crée une copie superficielle du Object actuel.

(Hérité de Object)
ToString()

Retourne une chaîne qui représente l'objet actuel.

(Hérité de Object)

Implémentations d’interfaces explicites

ICollection.Count

Obtient le nombre d’éléments contenus dans la collection.

ICollection.IsSynchronized

Obtient une valeur indiquant si l'accès à la collection est synchronisé (thread-safe).

ICollection.SyncRoot

Obtient un objet qui peut être utilisé pour synchroniser l’accès à la collection.

IEnumerable.GetEnumerator()

Retourne IEnumerator pour l'objet IDictionary.

Méthodes d’extension

Cast<TResult>(IEnumerable)

Effectue un cast des éléments d'un IEnumerable vers le type spécifié.

OfType<TResult>(IEnumerable)

Filtre les éléments d'un IEnumerable en fonction du type spécifié.

AsParallel(IEnumerable)

Active la parallélisation d'une requête.

AsQueryable(IEnumerable)

Convertit un IEnumerable en IQueryable.

S’applique à

Voir aussi