Partager via


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 le BrowsableAttribute jeu a été défini dans cette collection. Le deuxième exemple de code obtient la valeur réelle d’un DescriptionAttribute bouton. Les deux exemples nécessitent la button1 création d’un formulaire et textBox1 l’ont été. 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.";
      }
   }
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

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 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 requises. Si aucun attribut n’a de valeur par défaut, null il est retourné à partir de la propriété indexée qui prend 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

Nom Description
AttributeCollection()

Initialise une nouvelle instance de la classe AttributeCollection.

AttributeCollection(Attribute[])

Initialise une nouvelle instance de la classe AttributeCollection.

Champs

Nom Description
Empty

Spécifie une collection vide que vous pouvez utiliser, au lieu de en créer une nouvelle. Ce champ est en lecture seule.

Propriétés

Nom Description
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 avec le type spécifié.

Méthodes

Nom Description
Contains(Attribute)

Détermine si cette collection d’attributs a 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 objet existant AttributeCollection.

GetDefaultAttribute(Type)

Retourne la valeur par défaut Attribute d’un élément donné Type.

GetEnumerator()

Obtient un énumérateur pour cette collection.

GetHashCode()

Sert de fonction de hachage par défaut.

(Hérité de Object)
GetType()

Obtient la 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 Objectactuel.

(Hérité de Object)
ToString()

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

(Hérité de Object)

Implémentations d’interfaces explicites

Nom Description
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 une IEnumerator valeur pour le IDictionary.

Méthodes d’extension

Nom Description
AsParallel(IEnumerable)

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

AsQueryable(IEnumerable)

Convertit un IEnumerable en IQueryable.

Cast<TResult>(IEnumerable)

Convertit les éléments d’un IEnumerable en type spécifié.

OfType<TResult>(IEnumerable)

Filtre les éléments d’une IEnumerable en fonction d’un type spécifié.

S’applique à

Voir aussi