AttributeCollection 類別

定義

表示屬性的集合。Represents a collection of attributes.

public ref class AttributeCollection : System::Collections::ICollection
[System.Runtime.InteropServices.ComVisible(true)]
public class AttributeCollection : System.Collections.ICollection
type AttributeCollection = class
    interface ICollection
    interface IEnumerable
Public Class AttributeCollection
Implements ICollection
繼承
AttributeCollection
屬性
實作

範例

第一個程式碼範例會檢查是否BrowsableAttribute已在此集合中設定。The first code example checks to see whether the BrowsableAttribute has been set in this collection. 第二個DescriptionAttribute程式碼範例會取得按鈕的實際值。The second code example gets the actual value of the DescriptionAttribute for a button. 這兩個範例button1textBox1需要在表單上建立和。Both examples require that button1 and textBox1 have been created on a form. 使用屬性時,請確認已設定屬性,或存取其值。When using attributes, verify that an attribute has been set, or access its value.

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

備註

類別AttributeCollection是唯讀的,不會執行新增或移除屬性的方法。The AttributeCollection class is read-only; it does not implement methods to add or remove attributes. 您必須繼承自這個類別,才能執行這些方法。You must inherit from this class to implement these methods.

Count使用屬性,即可尋找存在於集合中的屬性數目。Use the Count property to find the number of attributes that exist in the collection.

您也可以使用這個類別的方法,查詢其內容的集合。You can also use the methods of this class to query the collection about its contents. Contains呼叫方法,以確認集合中是否存在指定的屬性或屬性陣列。Call the Contains method to verify that a specified attribute or attribute array exists in the collection. Matches呼叫方法,以確認集合中是否存在指定的屬性或屬性陣列,以及指定屬性的值是否與集合中的值相同。Call the Matches method to verify that a specified attribute or array of attributes exists in the collection, and that the values of the specified attributes are the same as the values in the collection.

雖然大部分的屬性都有預設值,但不需要預設值。While most attributes have default values, default values are not required. 如果屬性沒有預設值, null則會從接受類型的索引屬性傳回。If an attribute has no default value, null is returned from the indexed property that takes a type. 定義您自己的屬性時,您可以藉由提供不接受引數的函式,或定義名為 "Default" 之屬性類型的公用靜態欄位,來宣告預設值。When defining your own attributes, you can declare a default value by either providing a constructor that takes no arguments, or defining a public static field of your attribute type named "Default".

建構函式

AttributeCollection()

初始化 AttributeCollection 類別的新執行個體。Initializes a new instance of the AttributeCollection class.

AttributeCollection(Attribute[])

初始化 AttributeCollection 類別的新執行個體。Initializes a new instance of the AttributeCollection class.

欄位

Empty

指定可使用的空集合,而非建立新集合。Specifies an empty collection that you can use, rather than creating a new one. 此欄位為唯讀。This field is read-only.

屬性

Attributes

取得屬性集合。Gets the attribute collection.

Count

取得屬性的數目。Gets the number of attributes.

Item[Int32]

取得具有指定索引編號的屬性。Gets the attribute with the specified index number.

Item[Type]

取得具有指定型別的屬性。Gets the attribute with the specified type.

方法

Contains(Attribute)

判斷這個屬性集合是否具有指定屬性。Determines whether this collection of attributes has the specified attribute.

Contains(Attribute[])

判斷這個屬性集合是否包含屬性陣列中所有的指定屬性。Determines whether this attribute collection contains all the specified attributes in the attribute array.

CopyTo(Array, Int32)

複製集合至陣列,開始於指定的索引處。Copies the collection to an array, starting at the specified index.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
FromExisting(AttributeCollection, Attribute[])

從現有的 AttributeCollection 建立新 AttributeCollectionCreates a new AttributeCollection from an existing AttributeCollection.

GetDefaultAttribute(Type)

傳回指定 Attribute 的預設 TypeReturns the default Attribute of a given Type.

GetEnumerator()

取得這個集合的列舉程式。Gets an enumerator for this collection.

GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
Matches(Attribute)

判斷指定屬性是否和集合中的屬性相同。Determines whether a specified attribute is the same as an attribute in the collection.

Matches(Attribute[])

判斷指定陣列中的屬性是否與集合中的屬性相同。Determines whether the attributes in the specified array are the same as the attributes in the collection.

MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

ICollection.Count

取得集合所包含的項目數目。Gets the number of elements contained in the collection.

ICollection.IsSynchronized

取得值,指出對集合的存取是否為同步的 (安全執行緒)。Gets a value indicating whether access to the collection is synchronized (thread-safe).

ICollection.SyncRoot

取得物件,這個物件可以用來對集合進行同步存取。Gets an object that can be used to synchronize access to the collection.

IEnumerable.GetEnumerator()

傳回 IEnumeratorIDictionaryReturns an IEnumerator for the IDictionary.

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於

另請參閱