AttributeCollection クラス
アセンブリ: System (system.dll 内)


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.ComponentModel.AttributeCollection


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


AttributeCollection メンバ
System.ComponentModel 名前空間
Attribute
BrowsableAttribute
DescriptionAttribute
AttributeCollection クラス
アセンブリ: System.Web (system.web.dll 内)


属性の大文字小文字の区別は承継元の StateBag クラスが大文字小文字を区別するかどうかに依存します。属性は、値として String オブジェクトを返します。コレクション内に属性がない場合は、null 参照 (Visual Basic では Nothing) を返します。
HTML サーバー コントロールの属性には、すべての HTML サーバー コントロールが継承する Attributes プロパティを通じて、プログラムによりアクセスできます。.NET Framework では、HTML 属性は、その属性が属している HTML サーバー コントロールのプロパティのように扱われます。
すべての Web サーバーが継承する Attributes プロパティを通じて、Web サーバー コントロールに属性を追加することもできます。Web サーバー コントロールについて、このコレクションに格納されている属性は、指定されている Web サーバー コントロールの厳密に型指定されたプロパティには対応していません。

次に示すコードは、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


System.Web.UI.AttributeCollection


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- AttributeCollection クラスのページへのリンク