Atrybuty zdefiniowane przez użytkownika (C++/CLI i C++/CX)

C++/CLI i C++/CX umożliwiają tworzenie atrybutów specyficznych dla platformy, które rozszerzają metadane interfejsu, klasy lub struktury, metody, parametru lub wyliczenia. Te atrybuty różnią się od standardowych atrybutów języka C++.

Środowisko wykonawcze systemu Windows

Atrybuty języka C++/CX można stosować do właściwości, ale nie do konstruktorów lub metod.

Wymagania

Opcja kompilatora: /ZW

środowiska uruchomieniowe w trakcie wykonania

Informacje i składnia przedstawione w tym temacie są przeznaczone do zastępowania informacji przedstawionych w atrybucie.

Atrybut niestandardowy można zdefiniować, definiując typ i tworząc Attribute klasę bazową dla typu i opcjonalnie stosując AttributeUsageAttribute atrybut.

Aby uzyskać więcej informacji, zobacz:

Aby uzyskać informacje na temat podpisywania zestawów w programie Visual C++, zobacz Zestawy silnej nazwy (podpisywanie zestawów) (C++/CLI).

Wymagania

Opcja kompilatora: /clr

Przykłady

W poniższym przykładzie pokazano, jak zdefiniować atrybut niestandardowy.

// 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 {};

Poniższy przykład ilustruje niektóre ważne funkcje atrybutów niestandardowych. Na przykład w tym przykładzie pokazano typowe użycie atrybutów niestandardowych: utworzenie wystąpienia serwera, który może w pełni opisać się klientom.

// 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

Typ Object^ zastępuje typ danych wariantu. W poniższym przykładzie zdefiniowano atrybut niestandardowy, który przyjmuje tablicę Object^ jako parametry.

Argumenty atrybutów muszą być stałymi czasu kompilacji; w większości przypadków powinny być stałymi literałami.

Zobacz typeid , aby uzyskać informacje na temat zwracania wartości System::Type z bloku atrybutu niestandardowego.

// 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 {};

Środowisko uruchomieniowe wymaga, aby publiczna część klasy atrybutów niestandardowych można było serializować. Podczas tworzenia atrybutów niestandardowych nazwane argumenty atrybutu niestandardowego są ograniczone do stałych czasu kompilacji. (Pomyśl o nim jako sekwencję bitów dołączonych do układu klasy w metadanych).

// 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 {};

Zobacz też

Rozszerzenia składników dla platformy .NET i platformy uniwersalnej systemu Windows