AttributeCollection 类

表示属性 (Attribute) 的集合。

**命名空间:**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 类是只读的,它不实现添加或移除属性 (Attribute) 的方法。必须从此类继承以实现这些方法。

使用 Count 属性 (Property) 来查找集合中存在的属性 (Attribute) 的数目。

您还可以使用此类的方法来查询集合的内容。调用 Contains 方法来验证集合中是否存在指定的属性 (Attribute) 或属性 (Attribute) 数组。调用 Matches 方法来验证集合中是否存在指定的属性 (Attribute) 或属性 (Attribute) 数组以及所指定属性 (Attribute) 的值是否与集合中的值相同。

虽然大多数属性 (Attribute) 都具有默认值,但这并不是必需的。如果某个属性 (Attribute) 没有默认值,则会从采用类型的索引属性 (Property) 返回 空引用(在 Visual Basic 中为 Nothing)。当定义自己的特性时,可以通过以下两种方式声明默认值:提供不采用任何参数的构造函数,或者定义一个属性 (Attribute) 类型的名为“Default”的公共静态字段。

提示

应用于此类的 HostProtectionAttribute 属性 (Attribute) 具有以下 Resources 属性 (Property) 值:SynchronizationHostProtectionAttribute 不影响桌面应用程序(这些应用程序通常通过双击图标、键入命令或在浏览器中输入 URL 来启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护属性

示例

第一个代码示例检查以确定是否已在此集合中设置了 BrowsableAttribute。第二个代码示例获取按钮的 DescriptionAttribute 的实际值。这两个示例都要求已在窗体上创建 button1textBox1。当使用属性 (Attribute) 时,验证是否已设置了属性 (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 '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

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

AttributeCollection 成员
System.ComponentModel 命名空间
Attribute
BrowsableAttribute
DescriptionAttribute