Bagikan melalui


AttributeCollection Kelas

Definisi

Mewakili kumpulan atribut.

public ref class AttributeCollection : System::Collections::ICollection
public class AttributeCollection : System.Collections.ICollection
[System.Runtime.InteropServices.ComVisible(true)]
public class AttributeCollection : System.Collections.ICollection
type AttributeCollection = class
    interface ICollection
    interface IEnumerable
[<System.Runtime.InteropServices.ComVisible(true)>]
type AttributeCollection = class
    interface ICollection
    interface IEnumerable
Public Class AttributeCollection
Implements ICollection
Warisan
AttributeCollection
Atribut
Penerapan

Contoh

Contoh kode pertama memeriksa untuk melihat apakah BrowsableAttribute telah diatur dalam koleksi ini. Contoh kode kedua mendapatkan nilai aktual untuk DescriptionAttribute tombol . Kedua contoh mengharuskan dan button1textBox1 telah dibuat pada formulir. Saat menggunakan atribut, verifikasi bahwa atribut telah ditetapkan, atau akses nilainya.

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.";
      }
   }
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;

    textBox1.Text = attributes.Contains(myAttribute) ? "button1 has a browsable attribute." : "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;
   }
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

Keterangan

Kelas AttributeCollection bersifat baca-saja; tidak menerapkan metode untuk menambahkan atau menghapus atribut. Anda harus mewarisi dari kelas ini untuk menerapkan metode ini.

Count Gunakan properti untuk menemukan jumlah atribut yang ada dalam koleksi.

Anda juga dapat menggunakan metode kelas ini untuk mengkueri koleksi tentang kontennya. Contains Panggil metode untuk memverifikasi bahwa atribut atau array atribut tertentu ada dalam koleksi. Matches Panggil metode untuk memverifikasi bahwa atribut atau array atribut tertentu ada dalam koleksi, dan bahwa nilai atribut yang ditentukan sama dengan nilai dalam koleksi.

Meskipun sebagian besar atribut memiliki nilai default, nilai default tidak diperlukan. Jika atribut tidak memiliki nilai default, null dikembalikan dari properti terindeks yang mengambil jenis. Saat menentukan atribut Anda sendiri, Anda dapat mendeklarasikan nilai default dengan menyediakan konstruktor yang tidak mengambil argumen, atau menentukan bidang statis publik dari jenis atribut Anda bernama "Default".

Konstruktor

Nama Deskripsi
AttributeCollection()

Menginisialisasi instans baru dari kelas AttributeCollection.

AttributeCollection(Attribute[])

Menginisialisasi instans baru dari kelas AttributeCollection.

Bidang

Nama Deskripsi
Empty

Menentukan koleksi kosong yang dapat Anda gunakan, daripada membuat koleksi baru. Bidang ini hanya dapat dibaca.

Properti

Nama Deskripsi
Attributes

Mendapatkan koleksi atribut.

Count

Mendapatkan jumlah atribut.

Item[Int32]

Mendapatkan atribut dengan nomor indeks yang ditentukan.

Item[Type]

Mendapatkan atribut dengan jenis yang ditentukan.

Metode

Nama Deskripsi
Contains(Attribute)

Menentukan apakah kumpulan atribut ini memiliki atribut yang ditentukan.

Contains(Attribute[])

Menentukan apakah koleksi atribut ini berisi semua atribut yang ditentukan dalam array atribut.

CopyTo(Array, Int32)

Menyalin koleksi ke array, dimulai dari indeks yang ditentukan.

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
FromExisting(AttributeCollection, Attribute[])

Membuat baru AttributeCollection dari AttributeCollection.

GetDefaultAttribute(Type)

Mengembalikan default Attribute dari .Type

GetEnumerator()

Mendapatkan enumerator untuk koleksi ini.

GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetType()

Mendapatkan Type instans saat ini.

(Diperoleh dari Object)
Matches(Attribute)

Menentukan apakah atribut yang ditentukan sama dengan atribut dalam koleksi.

Matches(Attribute[])

Menentukan apakah atribut dalam array yang ditentukan sama dengan atribut dalam koleksi.

MemberwiseClone()

Membuat salinan dangkal dari Objectsaat ini.

(Diperoleh dari Object)
ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)

Implementasi Antarmuka Eksplisit

Nama Deskripsi
ICollection.Count

Mendapatkan jumlah elemen yang terkandung dalam koleksi.

ICollection.IsSynchronized

Mendapatkan nilai yang menunjukkan apakah akses ke koleksi disinkronkan (aman utas).

ICollection.SyncRoot

Mendapatkan objek yang dapat digunakan untuk menyinkronkan akses ke koleksi.

IEnumerable.GetEnumerator()

Mengembalikan IEnumerator untuk IDictionary.

Metode Ekstensi

Nama Deskripsi
AsParallel(IEnumerable)

Mengaktifkan paralelisasi kueri.

AsQueryable(IEnumerable)

Mengonversi IEnumerable menjadi IQueryable.

Cast<TResult>(IEnumerable)

Melemparkan elemen IEnumerable ke jenis yang ditentukan.

OfType<TResult>(IEnumerable)

Memfilter elemen IEnumerable berdasarkan jenis tertentu.

Berlaku untuk

Lihat juga