Freigeben über


AttributeCollection-Klasse

Stellt eine Auflistung von Attributen dar.

Namespace: System.ComponentModel
Assembly: System (in system.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Class AttributeCollection
    Implements ICollection, IEnumerable
'Usage
Dim instance As AttributeCollection
[ComVisibleAttribute(true)] 
public class AttributeCollection : ICollection, IEnumerable
[ComVisibleAttribute(true)] 
public ref class AttributeCollection : ICollection, IEnumerable
/** @attribute ComVisibleAttribute(true) */ 
public class AttributeCollection implements ICollection, IEnumerable
ComVisibleAttribute(true) 
public class AttributeCollection implements ICollection, IEnumerable

Hinweise

Die AttributeCollection-Klasse ist schreibgeschützt und implementiert keine Methoden zum Hinzufügen oder Entfernen von Attributen. Sie müssen diese Klasse vererben, um diese Methoden zu implementieren.

Mit der Count-Eigenschaft können Sie die Anzahl der Attribute ermitteln, die in der Auflistung vorhanden sind.

Mit den Methoden dieser Klasse können Sie auch den Inhalt der Auflistung abfragen. Rufen Sie die Contains-Methode auf, um zu überprüfen, ob ein angegebenes Attribut oder Array von Attributen in der Auflistung vorhanden ist. Rufen Sie die Matches-Methode auf, um zu überprüfen, ob ein angegebenes Attribut oder ein Array von Attributen in der Auflistung vorhanden ist und die Werte der angegebenen Attribute mit den Werten in der Auflistung identisch sind.

Obwohl die meisten Attribute über einen Standardwert verfügen, sind Standardwerte nicht erforderlich. Wenn ein Attribut keinen Standardwert hat, wird NULL (Nothing in Visual Basic) von der indizierten Eigenschaft zurückgegeben, die einen Typ akzeptiert. Wenn Sie eigene Attribute definieren, können Sie einen Standardwert deklarieren, indem Sie entweder einen Konstruktor bereitstellen, der keine Argumente akzeptiert, oder indem Sie ein öffentliches statisches Feld des Attributtyps mit dem Namen "Default" definieren.

Hinweis

Das auf diese Klasse angewendete HostProtectionAttribute-Attribut besitzt den Resources-Eigenschaftenwert Synchronization. Das HostProtectionAttribute hat keine Auswirkungen auf Desktopanwendungen (die normalerweise durch Doppelklicken auf ein Symbol, Eingeben eines Befehls oder eines URL in einem Browser gestartet werden). Weitere Informationen finden Sie unter der HostProtectionAttribute-Klasse oder unter SQL Server-Programmierung und Hostschutzattribute.

Beispiel

Im ersten Codebeispiel wird überprüft, ob die BrowsableAttribute-Klasse in der Auflistung festgelegt ist. Im zweiten Codebeispiel wird der tatsächliche Wert der DescriptionAttribute-Klasse für eine Schaltfläche abgerufen. Beide Beispiele erfordern, dass button1 und textBox1 in einem Formular erstellt wurden. Wenn Sie Attribute verwenden möchten, überprüfen Sie, ob ein Attribut festgelegt ist, oder greifen Sie auf seinen Wert zu.

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 'ContainsAttribute
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 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.set_Text("button1 has a browsable attribute.");
    }
    else {
        textBox1.set_Text("button1 does not have a browsable attribute.");
    }
} //ContainsAttribute
public function ContainsAttribute() {
    // Creates a new collection and assigns it the attributes for button1.
    var attributes : AttributeCollection;
    attributes = TypeDescriptor.GetAttributes(button1);
 
    // Sets an Attribute to the specific attribute.
    var myAttribute : BrowsableAttribute  = 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 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 'GetAttributeValue
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:
   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.get_Item(DesignerAttribute.class.ToType())));

    // Prints the value of the attribute in a text box.
    textBox1.set_Text(myDesigner.get_DesignerTypeName());
} //GetAttributeValue    
public function GetAttributeValue() {
    // Creates a new collection and assigns it the attributes for button1.
    var attributes : AttributeCollection ;
    attributes = TypeDescriptor.GetAttributes(button1);
 
    // Gets the designer attribute from the collection.
    var myDesigner : DesignerAttribute ; 
    myDesigner = DesignerAttribute(attributes[DesignerAttribute.GetType()]);
 
    // Prints the value of the attribute in a text box.
    if(myDesigner)
        textBox1.Text = myDesigner.DesignerTypeName;
 }

Vererbungshierarchie

System.Object
  System.ComponentModel.AttributeCollection

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

AttributeCollection-Member
System.ComponentModel-Namespace
Attribute
BrowsableAttribute
DescriptionAttribute