AttributeCollectionとは? わかりやすく解説

AttributeCollection クラス

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

名前空間: System.ComponentModel
アセンブリ: System (system.dll 内)
構文構文

<ComVisibleAttribute(True)> _
Public Class AttributeCollection
    Implements ICollection, IEnumerable
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
解説解説

AttributeCollection クラスは読み取り専用であり、属性の追加または削除を行うためのメソッドは実装していません。これらのメソッドを実装するには、このクラスから継承する必要があります。

コレクション内に存在する属性の数を調べるには、Count プロパティを使用します。

また、このクラスのメソッドを使用して、コレクションに対してその内容を照会することもできます。指定した属性または属性配列がコレクション内に存在するかどうかを確認するには、Contains メソッドを呼び出します。指定した属性または属性配列がコレクション内に存在し、その属性の値がコレクション内の値と同じかどうかを確認するには、Matches メソッドを呼び出します。

ほとんどの属性には既定値がありますが、必須ではありません。属性に既定値がない場合、型を取るインデックス付きプロパティは null 参照 (Visual Basic では Nothing) を返します。独自の属性を定義する場合は、引数をとらないコンストラクタを提供するか、"Default" という名前の属性型のパブリック静的フィールドを定義することによって、既定値を宣言できます。

メモメモ

このクラスに適用される HostProtectionAttribute 属性の Resources プロパティの値は、Synchronization です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的には、アイコンをダブルクリック、コマンドを入力、またはブラウザに URL を入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラスのトピックまたは「SQL Server プログラミングとホスト保護属性」を参照してください。

使用例使用例

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

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;
 }
継承階層継承階層
System.Object
  System.ComponentModel.AttributeCollection
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
AttributeCollection メンバ
System.ComponentModel 名前空間
Attribute
BrowsableAttribute
DescriptionAttribute

AttributeCollection クラス

ASP.NET サーバー コントロール要素の開始タグで宣言されたすべての属性へのオブジェクト モデル アクセスを提供します。このクラスは継承できません。

名前空間: System.Web.UI
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public NotInheritable Class
 AttributeCollection
Dim instance As AttributeCollection
public sealed class AttributeCollection
public ref class AttributeCollection sealed
public final class AttributeCollection
public final class AttributeCollection
解説解説
使用例使用例

次に示すコードは、myAttributeCollection という名前の新しい AttributeCollection を作成し、その後、ページがポストバック済みかどうかをチェックします。まだポストバックされていない場合は、2 つの属性をコレクションに追加し、コレクション内の属性の数を取得して、各属性のキーをページに書き込みます。ポストバック済みである場合は、変更後の属性数を単に取得し、コレクション内の各属性のキーと値をページに書き込みます。

        Dim myAttributeCollection As AttributeCollection
 = Nothing 

        Sub Page_Load(sender As Object,
 e As EventArgs)
            myAttributeCollection = New AttributeCollection(ViewState)
            Response.Write("<h3> AttributeCollection.AttributeCollection
 Sample </h3>")
            If Not IsPostBack Then
               myAttributeCollection.Add("Color",
 "Color.Red")
               myAttributeCollection.Add("BackColor",
 "Color.blue")
               Response.Write("Attribute Collection count before
 PostBack = " & _
myAttributeCollection.Count.ToString())
               Response.Write("<br><U><h4>Enumerating
 Attributes for " & _
                                       "CustomControl before PostBack</h4></U>")
               Dim keys As IEnumerator = myAttributeCollection.Keys.GetEnumerator()
               Dim i As Integer
 = 1
               Dim key As String
               While keys.MoveNext()
                  key = CType(keys.Current, String)
                  Response.Write(i.ToString() + ". "
 + key + "=" + myAttributeCollection(key) + "<br>")
                  i += 1
               End While
            Else
               Response.Write("Attribute Collection  count after
 PostBack = " + _
                                          myAttributeCollection.Count.ToString())
               Response.Write("<br><U><h4>Enumerating
 Attributes for " + _
                                       "CustomControl after PostBack</h4></U>")
               Dim keys As IEnumerator = myAttributeCollection.Keys.GetEnumerator()
               Dim i As Integer
 = 1
               Dim key As String
               While keys.MoveNext()
                  key = CType(keys.Current, String)
                  Response.Write(i.ToString() + ". "
 + key + "=" + myAttributeCollection(key) + "<br>")
                  i += 1
               End While
            End If
         End Sub
AttributeCollection myAttributeCollection = null;

void Page_Load(object sender,EventArgs e)
{
   myAttributeCollection = new AttributeCollection(ViewState);
   Response.Write("<h3> AttributeCollection.AttributeCollection Sample
 </h3>");
   if (!IsPostBack)
   {  
      myAttributeCollection.Add("Color" ,"Color.Red");
      myAttributeCollection.Add("BackColor","Color.blue");
      Response.Write("Attribute Collection  count before PostBack = " +
 myAttributeCollection.Count);
      Response.Write("<br><U><h4>Enumerating Attributes for
 CustomControl before PostBack</h4></U>");
      IEnumerator keys = myAttributeCollection.Keys.GetEnumerator();
      int i =1;
      String key;
      while (keys.MoveNext())
      {
         key = (String)keys.Current;
         Response.Write(i + ". "+key + "=" + myAttributeCollection[key]+"<br>");
         i++;
      }
   }
   else
   {
      Response.Write("Attribute Collection  count after PostBack = "+myAttributeCollection.Count);
      Response.Write("<br><U><h4>Enumerating Attributes for
 CustomControl after PostBack</h4></U>");
      IEnumerator keys = myAttributeCollection.Keys.GetEnumerator();
      int i =1;
      String key;
      while (keys.MoveNext())
      {
         key = (String)keys.Current;
         Response.Write(i + ". "+key + "=" + myAttributeCollection[key]+"<br>");
         i++;
      }
   }
}
System.Web.UI.AttributeCollection myAttributeCollection = null;
      
void Page_Load(Object sender,EventArgs e)
{
    myAttributeCollection = 
        new System.Web.UI.AttributeCollection(get_ViewState());
    get_Response().
        Write("<h3> AttributeCollection.AttributeCollection Sample </h3>");
    if (!get_IsPostBack()) {  
        myAttributeCollection.Add("Color" ,"Color.Red");
        myAttributeCollection.Add("BackColor","Color.blue");
        get_Response().Write("Attribute Collection  count before PostBack =
 " 
            + myAttributeCollection.get_Count());
        get_Response().Write("<br><U><h4>Enumerating Attributes
 for "
            + "CustomControl before PostBack</h4></U>");
        IEnumerator keys = myAttributeCollection.get_Keys().GetEnumerator();
        int i =1;
        String key;
        while (keys.MoveNext()) {
            key = (String)keys.get_Current();
            get_Response().Write(i + ". " + key + "=" 
                + myAttributeCollection.get_Item (key) + "<br>");
            i++;
        }
    }
    else {
        get_Response().Write("Attribute Collection  count after PostBack = "
            + myAttributeCollection.get_Count());
        get_Response().Write("<br><U><h4>Enumerating Attributes
 for "
            + "CustomControl after PostBack</h4></U>");
        IEnumerator keys = myAttributeCollection.get_Keys().GetEnumerator();
        int i =1;
        String key;
        while (keys.MoveNext()) {
            key = (String)keys.get_Current();
            get_Response().Write(i + ". " + key + "=" 
                + myAttributeCollection.get_Item(key)+"<br>");
            i++;
        }
    }
} //Page_Load
.NET Framework のセキュリティ.NET Framework のセキュリティ
継承階層継承階層
System.Object
  System.Web.UI.AttributeCollection
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

AttributeCollection コンストラクタ

AttributeCollection クラスの新しいインスタンスを初期化します。

名前空間: System.ComponentModel
アセンブリ: System (system.dll 内)
構文構文

public AttributeCollection (
    params Attribute[] attributes
)
public:
AttributeCollection (
    ... array<Attribute^>^ attributes
)
public AttributeCollection (
    Attribute[] attributes
)
public function AttributeCollection (
    ... attributes : Attribute[]
)

パラメータ

attributes

コレクションの属性を提供する Attribute 型の配列。

例外例外
例外の種類条件

ArgumentNullException

attributes が null 参照 (Visual Basic では Nothing) です。

使用例使用例

button1 の属性を使用して新しい AttributeCollection を作成するコード例を次に示します。この例は、フォーム上に button1 が作成されていることを前提としています。

Dim collection1 As AttributeCollection
collection1 = TypeDescriptor.GetAttributes(button1)
AttributeCollection collection1;
collection1 = TypeDescriptor.GetAttributes(button1);
}
AttributeCollection^ collection1;
collection1 = TypeDescriptor::GetAttributes( button1 );
    AttributeCollection collection1;
    collection1 = TypeDescriptor.GetAttributes(button1);
} //Method
 var collection1 : AttributeCollection;
 collection1 = TypeDescriptor.GetAttributes(button1);
 textBox1.Text = "The button1 has " + collection1.Count + " attributes";
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

AttributeCollection コンストラクタ

AttributeCollection クラスの新しいインスタンスを初期化します。

名前空間: System.Web.UI
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public Sub New ( _
    bag As StateBag _
)
Dim bag As StateBag

Dim instance As New AttributeCollection(bag)
public AttributeCollection (
    StateBag bag
)
public:
AttributeCollection (
    StateBag^ bag
)
public AttributeCollection (
    StateBag bag
)
public function AttributeCollection (
    bag : StateBag
)

パラメータ

bag

サーバー コントロールの開始タグの属性キーとその値を格納している StateBag オブジェクト。

使用例使用例

AttributeCollection コンストラクタを使用して、このクラスのインスタンスを作成し、myAttributeCollection という名前を付け、コントロールの ViewState プロパティをパラメータ引数として渡す例を次に示します。

myAttributeCollection = New AttributeCollection(ViewState)
myAttributeCollection = new AttributeCollection(ViewState);
myAttributeCollection = 
    new System.Web.UI.AttributeCollection(get_ViewState());
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
AttributeCollection クラス
AttributeCollection メンバ
System.Web.UI 名前空間
StateBag

AttributeCollection フィールド


AttributeCollection プロパティ


AttributeCollection プロパティ


AttributeCollection メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
明示的インターフェイスの実装明示的インターフェイスの実装
  名前 説明
インターフェイスの明示的な実装 System.Collections.IEnumerable.GetEnumerator IDictionary の IEnumerator を返します。
参照参照

関連項目

AttributeCollection クラス
System.ComponentModel 名前空間
Attribute
BrowsableAttribute
DescriptionAttribute

AttributeCollection メソッド


パブリック メソッドパブリック メソッド

( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド Add サーバー コントロールの AttributeCollection オブジェクトに属性を追加します。
パブリック メソッド AddAttributes 属性を HTML として ASP.NET サーバー コントロールに表示する HtmlTextWriter オブジェクトに AttributeCollection クラスの属性を追加します。
パブリック メソッド Clear サーバー コントロールの AttributeCollection オブジェクトからすべての属性を削除します。
パブリック メソッド Equals オーバーロードされます。 オーバーライドされます。 AttributeCollection コレクションの 2 つのインスタンスが等しいかどうかを示す値を返します。
パブリック メソッド GetHashCode オーバーライドされます。 対象のインスタンスのハッシュ コードを返します。
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 ( Object から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。)
パブリック メソッド Remove サーバー コントロールの AttributeCollection オブジェクトから属性を削除します。
パブリック メソッド Render 指定した HtmlTextWriter 出力ストリームに属性のコレクションを書き込みます。その後、出力ストリームは、そのコレクションが属する ASP.NET サーバー コントロールにそのコレクションを書き込みます。
パブリック メソッド ToString  現在の Object を表す String を返します。 ( Object から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

AttributeCollection クラス
System.Web.UI 名前空間
Attributes
Attributes

AttributeCollection メンバ

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

AttributeCollection データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド AttributeCollection AttributeCollection クラスの新しいインスタンスを初期化します。
パブリック フィールドパブリック フィールド
パブリック プロパティパブリック プロパティ
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
明示的インターフェイスの実装明示的インターフェイスの実装
  名前 説明
インターフェイスの明示的な実装 System.Collections.IEnumerable.GetEnumerator IDictionary の IEnumerator を返します。
インターフェイスの明示的な実装 System.Collections.ICollection.SyncRoot コレクションへのアクセスを同期するために使用できるオブジェクトを取得します。
参照参照

関連項目

AttributeCollection クラス
System.ComponentModel 名前空間
Attribute
BrowsableAttribute
DescriptionAttribute

AttributeCollection メンバ

ASP.NET サーバー コントロール要素の開始タグで宣言されたすべての属性へのオブジェクト モデル アクセスを提供します。このクラスは継承できません。

AttributeCollection データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド AttributeCollection AttributeCollection クラスの新しいインスタンスを初期化します。
パブリック プロパティパブリック プロパティ
パブリック メソッドパブリック メソッド
( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド Add サーバー コントロールの AttributeCollection オブジェクトに属性を追加します。
パブリック メソッド AddAttributes 属性を HTML として ASP.NET サーバー コントロールに表示する HtmlTextWriter オブジェクトに AttributeCollection クラスの属性を追加します。
パブリック メソッド Clear サーバー コントロールの AttributeCollection オブジェクトからすべての属性を削除します。
パブリック メソッド Equals オーバーロードされます。 オーバーライドされます。 AttributeCollection コレクションの 2 つのインスタンスが等しいかどうかを示す値を返します。
パブリック メソッド GetHashCode オーバーライドされます。 対象のインスタンスのハッシュ コードを返します。
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。)
パブリック メソッド Remove サーバー コントロールの AttributeCollection オブジェクトから属性を削除します。
パブリック メソッド Render 指定した HtmlTextWriter 出力ストリームに属性のコレクションを書き込みます。その後、出力ストリームは、そのコレクションが属する ASP.NET サーバー コントロールにそのコレクションを書き込みます。
パブリック メソッド ToString  現在の Object を表す String を返します。 (Object から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

AttributeCollection クラス
System.Web.UI 名前空間
Attributes
Attributes



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「AttributeCollection」の関連用語

AttributeCollectionのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



AttributeCollectionのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS