AttributeCollection AttributeCollection AttributeCollection AttributeCollection Class

定义

表示特性的集合。Represents a collection of attributes.

public ref class AttributeCollection : System::Collections::ICollection
[System.Runtime.InteropServices.ComVisible(true)]
public class AttributeCollection : System.Collections.ICollection
type AttributeCollection = class
    interface ICollection
    interface IEnumerable
Public Class AttributeCollection
Implements ICollection
继承
AttributeCollectionAttributeCollectionAttributeCollectionAttributeCollection
属性
实现

示例

第一个代码示例检查是否BrowsableAttribute已在此集合中设置。The first code example checks to see whether the BrowsableAttribute has been set in this collection. 第二个代码示例获取按钮的实际值DescriptionAttributeThe second code example gets the actual value of the DescriptionAttribute for a button. 这两个示例button1textBox1要求在窗体上创建和。Both examples require that button1 and textBox1 have been created on a form. 使用属性时,请验证是否已设置了属性,或者是否访问了属性的值。When using attributes, verify that an attribute has been set, or access its value.

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类是只读的; 它不实现添加或移除特性的方法。The AttributeCollection class is read-only; it does not implement methods to add or remove attributes. 必须从此类继承以实现这些方法。You must inherit from this class to implement these methods.

Count使用属性可查找集合中存在的属性数。Use the Count property to find the number of attributes that exist in the collection.

你还可以使用此类的方法来查询集合中有关其内容的信息。You can also use the methods of this class to query the collection about its contents. Contains调用方法以验证集合中是否存在指定的特性或特性数组。Call the Contains method to verify that a specified attribute or attribute array exists in the collection. Matches调用方法以验证集合中是否存在指定的特性或特性数组,以及指定特性的值是否与集合中的值相同。Call the Matches method to verify that a specified attribute or array of attributes exists in the collection, and that the values of the specified attributes are the same as the values in the collection.

虽然大多数属性都具有默认值,但不需要默认值。While most attributes have default values, default values are not required. 如果特性没有默认值, null则从采用类型的索引属性返回。If an attribute has no default value, null is returned from the indexed property that takes a type. 定义自己的属性时,可以通过提供不带参数的构造函数,或者定义名为 "Default" 的属性类型的公共静态字段,来声明默认值。When defining your own attributes, you can declare a default value by either providing a constructor that takes no arguments, or defining a public static field of your attribute type named "Default".

构造函数

AttributeCollection() AttributeCollection() AttributeCollection() AttributeCollection()

初始化 AttributeCollection 类的新实例。Initializes a new instance of the AttributeCollection class.

AttributeCollection(Attribute[]) AttributeCollection(Attribute[]) AttributeCollection(Attribute[]) AttributeCollection(Attribute[])

初始化 AttributeCollection 类的新实例。Initializes a new instance of the AttributeCollection class.

字段

Empty Empty Empty Empty

指定一个可以使用的空集合,而不是创建一个新集合。Specifies an empty collection that you can use, rather than creating a new one. 此字段为只读。This field is read-only.

属性

Attributes Attributes Attributes Attributes

获取特性集合。Gets the attribute collection.

Count Count Count Count

获取特性的数目。Gets the number of attributes.

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

获取具有指定索引号的特性。Gets the attribute with the specified index number.

Item[Type] Item[Type] Item[Type] Item[Type]

获取具有指定类型的特性。Gets the attribute with the specified type.

方法

Contains(Attribute) Contains(Attribute) Contains(Attribute) Contains(Attribute)

确定该特性集合是否具有指定的特性。Determines whether this collection of attributes has the specified attribute.

Contains(Attribute[]) Contains(Attribute[]) Contains(Attribute[]) Contains(Attribute[])

确定该特性集合是否包含特性数组中所有指定的特性。Determines whether this attribute collection contains all the specified attributes in the attribute array.

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

从指定的索引开始,将集合复制到数组。Copies the collection to an array, starting at the specified index.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
FromExisting(AttributeCollection, Attribute[]) FromExisting(AttributeCollection, Attribute[]) FromExisting(AttributeCollection, Attribute[]) FromExisting(AttributeCollection, Attribute[])

从现有的 AttributeCollection 创建新的 AttributeCollectionCreates a new AttributeCollection from an existing AttributeCollection.

GetDefaultAttribute(Type) GetDefaultAttribute(Type) GetDefaultAttribute(Type) GetDefaultAttribute(Type)

返回给定 Attribute 的默认 TypeReturns the default Attribute of a given Type.

GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

获取此集合的枚举器。Gets an enumerator for this collection.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
Matches(Attribute) Matches(Attribute) Matches(Attribute) Matches(Attribute)

确定指定的特性是否与集合中的特性相同。Determines whether a specified attribute is the same as an attribute in the collection.

Matches(Attribute[]) Matches(Attribute[]) Matches(Attribute[]) Matches(Attribute[])

确定指定数组中的特性是否与集合中的特性相同。Determines whether the attributes in the specified array are the same as the attributes in the collection.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

显式界面实现

ICollection.Count ICollection.Count ICollection.Count ICollection.Count

获取集合中包含的元素数。Gets the number of elements contained in the collection.

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

获取一个值,该值指示对集合的访问是否为同步的(线程安全)。Gets a value indicating whether access to the collection is synchronized (thread-safe).

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

获取可用于同步对集合的访问的对象。Gets an object that can be used to synchronize access to the collection.

IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator()

返回 IEnumeratorIDictionaryReturns an IEnumerator for the IDictionary.

扩展方法

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于

另请参阅