AttributeCollection 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示特性的集合。
public ref class AttributeCollection : System::Collections::ICollectionpublic class AttributeCollection : System.Collections.ICollection[System.Runtime.InteropServices.ComVisible(true)]
public class AttributeCollection : System.Collections.ICollectiontype AttributeCollection = class
    interface ICollection
    interface IEnumerable[<System.Runtime.InteropServices.ComVisible(true)>]
type AttributeCollection = class
    interface ICollection
    interface IEnumerablePublic Class AttributeCollection
Implements ICollection- 继承
- 
				AttributeCollection
- 属性
- 实现
示例
第一个代码示例检查 BrowsableAttribute 是否已在此集合中设置 。 第二个代码示例获取按钮的实际 DescriptionAttribute 值。 这两个示例都需要 button1 在窗体上创建 和 textBox1 。 使用特性时,请验证是否已设置特性,或访问其值。
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 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
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[typeof(DesignerAttribute)];
    // Prints the value of the attribute in a text box.
    textBox1.Text = myDesigner.DesignerTypeName;
 }
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
注解
类 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) | |
| GetEnumerator() | 获取此集合的枚举器。 | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| Matches(Attribute) | 确定指定的特性是否与集合中的特性相同。 | 
| Matches(Attribute[]) | 确定指定数组中的特性是否与集合中的特性相同。 | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
显式接口实现
| ICollection.Count | 获取集合中包含的元素数。 | 
| ICollection.IsSynchronized | 获取一个值,该值指示对集合的访问是否已同步(线程安全)。 | 
| ICollection.SyncRoot | 获取可用于同步对集合的访问的对象。 | 
| IEnumerable.GetEnumerator() | 返回 IEnumerator 的 IDictionary。 | 
扩展方法
| Cast<TResult>(IEnumerable) | 将 IEnumerable 的元素强制转换为指定的类型。 | 
| OfType<TResult>(IEnumerable) | 根据指定类型筛选 IEnumerable 的元素。 | 
| AsParallel(IEnumerable) | 启用查询的并行化。 | 
| AsQueryable(IEnumerable) | 将 IEnumerable 转换为 IQueryable。 |