AttributeCollection.Item[] 属性

定义

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

重载

名称 说明
Item[Int32]

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

Item[Type]

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

Item[Int32]

Source:
AttributeCollection.cs
Source:
AttributeCollection.cs
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
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

Type Attribute要从集合中获取的项。

属性值

具有 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

注解

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

另请参阅

适用于