英語で読む

次の方法で共有


AttributeCollection クラス

定義

属性のコレクションを表します。

C#
public class AttributeCollection : System.Collections.ICollection
C#
[System.Runtime.InteropServices.ComVisible(true)]
public class AttributeCollection : System.Collections.ICollection
継承
AttributeCollection
属性
実装

最初のコード例では、 がこのコレクションに設定されているかどうかを BrowsableAttribute 確認します。 2 番目のコード例では、ボタンの の実際の DescriptionAttribute 値を取得します。 どちらの例でも、 と button1textBox1 がフォームに作成されている必要があります。 属性を使用する場合は、属性が設定されていることを確認するか、その値にアクセスします。

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)

IEnumerableIQueryable に変換します。

適用対象

製品 バージョン
.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

こちらもご覧ください