AttributeCollection 類別

定義

表示屬性的集合。

C#
public class AttributeCollection : System.Collections.ICollection
C#
[System.Runtime.InteropServices.ComVisible(true)]
public class AttributeCollection : System.Collections.ICollection
繼承
AttributeCollection
屬性
實作

範例

第一個程式代碼範例會檢查 BrowsableAttribute 是否已在此集合中設定 。 第二個程式代碼範例會取得按鈕的實際值 DescriptionAttribute 。 這兩個範例都需要 button1 在表單上建立 和 textBox1 。 使用屬性時,請確認已設定屬性,或存取其值。

C#
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.";
 }
C#
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;
 }

備註

類別 AttributeCollection 是只讀的;它不會實作方法來新增或移除屬性。 您必須繼承自這個類別,才能實作這些方法。

Count使用屬性來尋找存在於集合中的屬性數目。

您也可以使用這個類別的 方法來查詢集合中有關其內容的資訊。 Contains呼叫 方法,以確認集合中有指定的屬性或屬性陣列。 Matches呼叫 方法,以確認集合中有指定的屬性或屬性陣列,而且指定屬性的值與集合中的值相同。

雖然大部分屬性都有預設值,但不需要預設值。 如果屬性沒有預設值, null 則會從採用類型的索引屬性傳回。 定義您自己的屬性時,您可以藉由提供不採用自變數的建構函式,或定義名為 “Default” 之屬性類型的公用靜態字段來宣告預設值。

建構函式

AttributeCollection()

初始化 AttributeCollection 類別的新執行個體。

AttributeCollection(Attribute[])

初始化 AttributeCollection 類別的新執行個體。

欄位

Empty

指定可使用的空集合,而非建立新集合。 此欄位為唯讀。

屬性

Attributes

取得屬性集合。

Count

取得屬性的數目。

Item[Int32]

取得具有指定索引編號的屬性。

Item[Type]

取得具有指定型別的屬性。

方法

Contains(Attribute)

判斷這個屬性集合是否具有指定屬性。

Contains(Attribute[])

判斷這個屬性集合是否包含屬性陣列中所有的指定屬性。

CopyTo(Array, Int32)

複製集合至陣列,開始於指定的索引處。

Equals(Object)

判斷指定的物件是否等於目前的物件。

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

從現有的 AttributeCollection 建立新 AttributeCollection

GetDefaultAttribute(Type)

傳回指定 Attribute 的預設 Type

GetEnumerator()

取得這個集合的列舉程式。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
Matches(Attribute)

判斷指定屬性是否和集合中的屬性相同。

Matches(Attribute[])

判斷指定陣列中的屬性是否與集合中的屬性相同。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

ICollection.Count

取得集合所包含的項目數目。

ICollection.IsSynchronized

取得值,指出對集合的存取是否為同步的 (安全執行緒)。

ICollection.SyncRoot

取得物件,這個物件可以用來對集合進行同步存取。

IEnumerable.GetEnumerator()

傳回 IEnumeratorIDictionary

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另請參閱