Share via


Atribut yang Ditentukan Pengguna (C++/CLI dan C++/CX)

C++/CLI dan C++/CX memungkinkan Anda membuat atribut khusus platform yang memperluas metadata antarmuka, kelas atau struktur, metode, parameter, atau enumerasi. Atribut ini berbeda dari atribut C++ standar.

Windows Runtime

Anda dapat menerapkan atribut C++/CX ke properti, tetapi tidak untuk konstruktor atau metode.

Persyaratan

Opsi pengkompilasi: /ZW

Runtime Bahasa Umum

Informasi dan sintaks yang disajikan dalam topik ini dimaksudkan untuk menggantikan informasi yang disajikan dalam atribut.

Anda dapat menentukan atribut kustom dengan menentukan jenis dan membuat Attribute kelas dasar untuk jenis dan secara opsional menerapkan AttributeUsageAttribute atribut.

Untuk informasi selengkapnya, lihat:

Untuk informasi tentang rakitan penandatanganan di Visual C++, lihat Rakitan Nama Kuat (Penandatanganan Rakitan) (C++/CLI).

Persyaratan

Opsi pengkompilasi: /clr

Contoh

Contoh berikut menunjukkan cara menentukan atribut kustom.

// user_defined_attributes.cpp
// compile with: /clr /c
using namespace System;

[AttributeUsage(AttributeTargets::All)]
ref struct Attr : public Attribute {
   Attr(bool i){}
   Attr(){}
};

[Attr]
ref class MyClass {};

Contoh berikut mengilustrasikan beberapa fitur penting atribut kustom. Misalnya, contoh ini menunjukkan penggunaan umum atribut kustom: membuat instans server yang dapat sepenuhnya menggambarkan dirinya sendiri kepada klien.

// extending_metadata_b.cpp
// compile with: /clr
using namespace System;
using namespace System::Reflection;

public enum class Access { Read, Write, Execute };

// Defining the Job attribute:
[AttributeUsage(AttributeTargets::Class, AllowMultiple=true )]
public ref class Job : Attribute {
public:
   property int Priority {
      void set( int value ) { m_Priority = value; }
      int get() { return m_Priority; }
   }

   // You can overload constructors to specify Job attribute in different ways
   Job() { m_Access = Access::Read; }
   Job( Access a ) { m_Access = a; }
   Access m_Access;

protected:
   int m_Priority;
};

interface struct IService {
   void Run();
};

   // Using the Job attribute:
   // Here we specify that QueryService is to be read only with a priority of 2.
   // To prevent namespace collisions, all custom attributes implicitly
   // end with "Attribute".

[Job( Access::Read, Priority=2 )]
ref struct QueryService : public IService {
   virtual void Run() {}
};

// Because we said AllowMultiple=true, we can add multiple attributes
[Job(Access::Read, Priority=1)]
[Job(Access::Write, Priority=3)]
ref struct StatsGenerator : public IService {
   virtual void Run( ) {}
};

int main() {
   IService ^ pIS;
   QueryService ^ pQS = gcnew QueryService;
   StatsGenerator ^ pSG = gcnew StatsGenerator;

   //  use QueryService
   pIS = safe_cast<IService ^>( pQS );

   // use StatsGenerator
   pIS = safe_cast<IService ^>( pSG );

   // Reflection
   MemberInfo ^ pMI = pIS->GetType();
   array <Object ^ > ^ pObjs = pMI->GetCustomAttributes(false);

   // We can now quickly and easily view custom attributes for an
   // Object through Reflection */
   for( int i = 0; i < pObjs->Length; i++ ) {
      Console::Write("Service Priority = ");
      Console::WriteLine(static_cast<Job^>(pObjs[i])->Priority);
      Console::Write("Service Access = ");
      Console::WriteLine(static_cast<Job^>(pObjs[i])->m_Access);
   }
}
Service Priority = 0

Service Access = Write

Service Priority = 3

Service Access = Write

Service Priority = 1

Service Access = Read

Jenis menggantikan Object^ jenis data varian. Contoh berikut mendefinisikan atribut kustom yang mengambil array Object^ sebagai parameter.

Argumen atribut harus berupa konstanta waktu kompilasi; dalam kebanyakan kasus, mereka harus menjadi literal konstan.

Lihat typeid untuk informasi tentang cara mengembalikan nilai Sistem::Ketik dari blok atribut kustom.

// extending_metadata_e.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Class | AttributeTargets::Method)]
public ref class AnotherAttr : public Attribute {
public:
   AnotherAttr(array<Object^>^) {}
   array<Object^>^ var1;
};

// applying the attribute
[ AnotherAttr( gcnew array<Object ^> { 3.14159, "pi" }, var1 = gcnew array<Object ^> { "a", "b" } ) ]
public ref class SomeClass {};

Runtime mengharuskan bagian publik dari kelas atribut kustom harus dapat diserialisasikan. Saat menulis atribut kustom, argumen bernama atribut kustom Anda terbatas pada konstanta waktu kompilasi. (Anggap saja sebagai urutan bit yang ditambahkan ke tata letak kelas Anda dalam metadata.)

// extending_metadata_f.cpp
// compile with: /clr /c
using namespace System;
ref struct abc {};

[AttributeUsage( AttributeTargets::All )]
ref struct A : Attribute {
   A( Type^ ) {}
   A( String ^ ) {}
   A( int ) {}
};

[A( abc::typeid )]
ref struct B {};

Baca juga

Ekstensi Komponen untuk .NET dan UWP