Sdílet prostřednictvím


AttributeCollection Třída

Definice

Představuje kolekci atributů.

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
Dědičnost
AttributeCollection
Atributy
Implementuje

Příklady

První příklad kódu zkontroluje, zda BrowsableAttribute byla nastavena v této kolekci. Druhý příklad kódu získá skutečnou DescriptionAttribute hodnotu tlačítka. Oba příklady vyžadují, aby button1 byly vytvořeny ve textBox1 formuláři. Při použití atributů ověřte, že byl atribut nastaven nebo přistupovat k jeho hodnotě.

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

Poznámky

Třída AttributeCollection je jen pro čtení; neimplementuje metody pro přidání nebo odebrání atributů. Chcete-li tyto metody implementovat, je nutné dědit z této třídy.

Count Pomocí vlastnosti vyhledejte počet atributů, které existují v kolekci.

Metody této třídy můžete také použít k dotazování kolekce o jejím obsahu. Contains Voláním metody ověřte, že v kolekci existuje zadaný atribut nebo pole atributů. Matches Volání metody k ověření, zda zadaný atribut nebo pole atributů existuje v kolekci a že hodnoty zadaných atributů jsou stejné jako hodnoty v kolekci.

I když většina atributů má výchozí hodnoty, výchozí hodnoty nejsou povinné. Pokud atribut nemá výchozí hodnotu, null vrátí se z indexované vlastnosti, která přebírá typ. Při definování vlastních atributů můžete deklarovat výchozí hodnotu zadáním konstruktoru, který nepřijímá žádné argumenty, nebo definováním veřejného statického pole typu atributu s názvem Default.

Konstruktory

Name Description
AttributeCollection()

Inicializuje novou instanci AttributeCollection třídy.

AttributeCollection(Attribute[])

Inicializuje novou instanci AttributeCollection třídy.

Pole

Name Description
Empty

Určuje prázdnou kolekci, kterou můžete použít místo vytvoření nové kolekce. Toto pole je jen pro čtení.

Vlastnosti

Name Description
Attributes

Získá kolekci atributů.

Count

Získá počet atributů.

Item[Int32]

Získá atribut se zadaným číslem indexu.

Item[Type]

Získá atribut se zadaným typem.

Metody

Name Description
Contains(Attribute)

Určuje, zda má tato kolekce atributů zadaný atribut.

Contains(Attribute[])

Určuje, zda tato kolekce atributů obsahuje všechny zadané atributy v poli atributů.

CopyTo(Array, Int32)

Zkopíruje kolekci do pole počínaje zadaným indexem.

Equals(Object)

Určuje, zda je zadaný objekt roven aktuálnímu objektu.

(Zděděno od Object)
FromExisting(AttributeCollection, Attribute[])

Vytvoří novou AttributeCollection z existujícího AttributeCollectionobjektu .

GetDefaultAttribute(Type)

Vrátí výchozí hodnotu Attribute daného Typeargumentu .

GetEnumerator()

Získá enumerátor pro tuto kolekci.

GetHashCode()

Slouží jako výchozí funkce hash.

(Zděděno od Object)
GetType()

Získá Type aktuální instance.

(Zděděno od Object)
Matches(Attribute)

Určuje, zda je zadaný atribut stejný jako atribut v kolekci.

Matches(Attribute[])

Určuje, zda jsou atributy v zadaném poli stejné jako atributy v kolekci.

MemberwiseClone()

Vytvoří mělkou kopii aktuálního Object.

(Zděděno od Object)
ToString()

Vrátí řetězec, který představuje aktuální objekt.

(Zděděno od Object)

Explicitní implementace rozhraní

Name Description
ICollection.Count

Získá počet prvků obsažených v kolekci.

ICollection.IsSynchronized

Získá hodnotu určující, zda je přístup k kolekci synchronizován (thread-safe).

ICollection.SyncRoot

Získá objekt, který lze použít k synchronizaci přístupu k kolekci.

IEnumerable.GetEnumerator()

IEnumerator Vrátí hodnotu pro IDictionaryhodnotu .

Metody rozšíření

Name Description
AsParallel(IEnumerable)

Umožňuje paralelizaci dotazu.

AsQueryable(IEnumerable)

Převede IEnumerable na IQueryable.

Cast<TResult>(IEnumerable)

Přetypuje prvky IEnumerable na zadaný typ.

OfType<TResult>(IEnumerable)

Filtruje prvky IEnumerable na základě zadaného typu.

Platí pro

Viz také