Share via


AttributeCollection.Item[] 屬性

定義

取得具有指定索引的屬性。

多載

Item[Int32]

取得具有指定索引編號的屬性。

Item[Type]

取得具有指定型別的屬性。

Item[Int32]

來源:
AttributeCollection.cs
來源:
AttributeCollection.cs
來源:
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 名稱。 它會假設 button1textBox1 已在表單上建立。

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]

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

備註

如果屬性不存在於集合中,這個屬性會傳回屬性類型的預設值。

另請參閱

適用於