AttributeCollection.Item[] 属性

定义

获取具有指定索引的特性。

重载

Item[Int32]

获取具有指定索引号的特性。

Item[Type]

获取具有指定类型的特性。

Item[Int32]

Source:
AttributeCollection.cs
Source:
AttributeCollection.cs
Source:
AttributeCollection.cs

获取具有指定索引号的特性。

public:
 virtual property Attribute ^ default[int] { Attribute ^ get(int index); };
public virtual Attribute this[int index] { get; }
member this.Item(int) : Attribute
Default Public Overridable ReadOnly Property Item(index As Integer) As Attribute

参数

index
Int32

AttributeCollection 的从零开始的索引。

属性值

具有指定索引号的 Attribute

示例

下面的代码示例使用 Item[] 属性在文本框中打印由索引号指定的 的名称 Attribute 。 由于索引号从零开始,因此此代码示例在文本框中打印第二 Attribute 个索引号的名称。 它假定 button1 已在窗体上创建 和 textBox1

private:
   void PrintIndexItem()
   {
      // Creates a new collection and assigns it the attributes for button1.
      AttributeCollection^ attributes;
      attributes = TypeDescriptor::GetAttributes( button1 );
      
      // Prints the second attribute's name.
      textBox1->Text = attributes[ 1 ]->ToString();
   }
private void PrintIndexItem() {
    // Creates a new collection and assigns it the attributes for button1.
    AttributeCollection attributes;
    attributes = TypeDescriptor.GetAttributes(button1);

    // Prints the second attribute's name.
    textBox1.Text = attributes[1].ToString();
 }
Private Sub PrintIndexItem
    ' Creates a new collection and assigns it the attributes for button1.
    Dim attributes As AttributeCollection
    attributes = TypeDescriptor.GetAttributes(button1)

    ' Prints the second attribute's name.
    textBox1.Text = attributes(1).ToString
End Sub

注解

索引号从零开始。 因此,必须从特定 Attribute 的数字位置中减去 1 才能访问该 Attribute。 例如,若要获取第三 Attribute个 ,需要指定 myColl[2]

另请参阅

适用于

Item[Type]

Source:
AttributeCollection.cs
Source:
AttributeCollection.cs
Source:
AttributeCollection.cs

获取具有指定类型的特性。

public:
 virtual property Attribute ^ default[Type ^] { Attribute ^ get(Type ^ attributeType); };
public virtual Attribute this[Type attributeType] { get; }
public virtual Attribute? this[Type attributeType] { get; }
member this.Item(Type) : Attribute
Default Public Overridable ReadOnly Property Item(attributeType As Type) As Attribute

参数

attributeType
Type

要从集合中获取的 TypeAttribute

属性值

具有指定类型的 Attribute;如果该特性不存在,则为该特性类型的默认值。

示例

下面的代码示例从 集合中获取 DesignerAttribute 并打印其值。 它假定 button1 已在窗体上创建了 和 textBox1

若要运行此代码示例,必须提供完全限定的程序集名称。 有关如何获取完全限定程序集名称的信息,请参阅 程序集名称

void PrintIndexItem2()
{
   
   // 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;
   
   // You must supply a valid fully qualified assembly name here. 
   myDesigner = dynamic_cast<DesignerAttribute^>(attributes[ Type::GetType(  "Assembly text name, Version, Culture, PublicKeyToken" ) ]);
   textBox1->Text = myDesigner->DesignerTypeName;
}
private void PrintIndexItem2() {
    // 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; 
    // You must supply a valid fully qualified assembly name here. 
    myDesigner = (DesignerAttribute)attributes[Type.GetType("Assembly text name, Version, Culture, PublicKeyToken")];
    textBox1.Text = myDesigner.DesignerTypeName;
 }
Private Sub PrintIndexItem2
    ' 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
            ' You must supply a valid fully qualified assembly name here. 
    myDesigner = CType(attributes(Type.GetType("Assembly text name, Version, Culture, PublicKeyToken")), DesignerAttribute)
    textBox1.Text = myDesigner.DesignerTypeName
End Sub

注解

如果集合中不存在该属性,则此属性返回属性类型的默认值。

另请参阅

适用于