AttributeCollection Klasa

Definicja

Reprezentuje kolekcję atrybutów.

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
Dziedziczenie
AttributeCollection
Atrybuty
Implementuje

Przykłady

Pierwszy przykład kodu sprawdza, czy BrowsableAttribute element został ustawiony w tej kolekcji. Drugi przykład kodu pobiera rzeczywistą wartość DescriptionAttribute przycisku. Oba przykłady wymagają tego button1 i textBox1 zostały utworzone w formularzu. W przypadku używania atrybutów sprawdź, czy atrybut został ustawiony lub uzyskaj dostęp do jego wartości.

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

Uwagi

Klasa AttributeCollection jest tylko do odczytu. Nie implementuje metod dodawania ani usuwania atrybutów. Aby zaimplementować te metody, należy dziedziczyć z tej klasy.

Użyj właściwości , Count aby znaleźć liczbę atrybutów, które istnieją w kolekcji.

Możesz również użyć metod tej klasy, aby wykonać zapytanie dotyczące kolekcji o jej zawartości. Wywołaj metodę, Contains aby sprawdzić, czy w kolekcji istnieje określony atrybut lub tablica atrybutów. Wywołaj metodę Matches , aby sprawdzić, czy w kolekcji istnieje określony atrybut lub tablica atrybutów, a wartości określonych atrybutów są takie same jak wartości w kolekcji.

Chociaż większość atrybutów ma wartości domyślne, wartości domyślne nie są wymagane. Jeśli atrybut nie ma wartości domyślnej, null jest zwracany z indeksowanej właściwości, która przyjmuje typ. Podczas definiowania własnych atrybutów można zadeklarować wartość domyślną, podając konstruktor, który nie przyjmuje argumentów, lub definiując publiczne pole statyczne typu atrybutu o nazwie "Default".

Konstruktory

AttributeCollection()

Inicjuje nowe wystąpienie klasy AttributeCollection.

AttributeCollection(Attribute[])

Inicjuje nowe wystąpienie klasy AttributeCollection.

Pola

Empty

Określa pustą kolekcję, której można użyć, zamiast tworzyć nowe. To pole jest tylko do odczytu.

Właściwości

Attributes

Pobiera kolekcję atrybutów.

Count

Pobiera liczbę atrybutów.

Item[Int32]

Pobiera atrybut z określonym numerem indeksu.

Item[Type]

Pobiera atrybut o określonym typie.

Metody

Contains(Attribute)

Określa, czy ta kolekcja atrybutów ma określony atrybut.

Contains(Attribute[])

Określa, czy ta kolekcja atrybutów zawiera wszystkie określone atrybuty w tablicy atrybutów.

CopyTo(Array, Int32)

Kopiuje kolekcję do tablicy, zaczynając od określonego indeksu.

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
FromExisting(AttributeCollection, Attribute[])

Tworzy nowy AttributeCollection element na podstawie istniejącego AttributeCollectionelementu .

GetDefaultAttribute(Type)

Zwraca wartość domyślną Attribute danego Typeelementu .

GetEnumerator()

Pobiera moduł wyliczający dla tej kolekcji.

GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
Matches(Attribute)

Określa, czy określony atrybut jest taki sam jak atrybut w kolekcji.

Matches(Attribute[])

Określa, czy atrybuty w określonej tablicy są takie same jak atrybuty w kolekcji.

MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Jawne implementacje interfejsu

ICollection.Count

Pobiera liczbę elementów zawartych w kolekcji.

ICollection.IsSynchronized

Pobiera wartość wskazującą, czy dostęp do kolekcji jest synchronizowany (bezpieczny wątkowo).

ICollection.SyncRoot

Pobiera obiekt, który może służyć do synchronizowania dostępu do kolekcji.

IEnumerable.GetEnumerator()

Zwraca element IEnumerator dla elementu IDictionary.

Metody rozszerzania

Cast<TResult>(IEnumerable)

Rzutuje elementy elementu IEnumerable na określony typ.

OfType<TResult>(IEnumerable)

Filtruje elementy elementu IEnumerable na podstawie określonego typu.

AsParallel(IEnumerable)

Umożliwia równoległość zapytania.

AsQueryable(IEnumerable)

Konwertuje element IEnumerable na .IQueryable

Dotyczy

Zobacz też