다음을 통해 공유


사용자 정의 특성(C++ 구성 요소 확장)

사용자 지정 특성을 인터페이스, 클래스 또는 구조체, 메서드, 매개 변수 또는 열거형의 메타 데이터를 확장할 수 있습니다.

모든 런타임

모든 런타임 사용자 지정 특성을 지원합니다.

Windows 런타임

C + + /cli CX 특성은 속성을 지원 하지만 생성자 또는 메서드에 특성이 없습니다.

yd21828z.collapse_all(ko-kr,VS.110).gif요구 사항

컴파일러 옵션:/ZW

공용 언어 런타임

사용자 지정 특성 메타 데이터 관리 요소를 확장할 수 있습니다.자세한 내용은 특성을 사용하여 메타데이터 확장을 참조하십시오.

yd21828z.collapse_all(ko-kr,VS.110).gif설명

정보 및이 항목에 제시 된 구문을 제공 되는 정보를 대체 하도록 것 특성.

형식을 정의 하 고 하 여 사용자 지정 특성을 정의할 수 있습니다 Attribute 기본 클래스 형식에 대 한 선택적으로 적용 하는 AttributeUsageAttribute 특성.

예를 들어, in Microsoft Transaction Server (MTS) 1.0, 동작은 트랜잭션, 동기화에 대 한 로드 균형 조정, 고 등을 통해 ODL 사용자 지정 특성을 사용 하 여 형식 라이브러리에 삽입 하는 사용자 지정 guid가 지정 되었습니다.따라서 클라이언트가 MTS 서버 형식 라이브러리를 읽어의 특징을 확인할 수 있습니다..NET Framework 아날로그 형식 라이브러리의 메타 데이터를이 고 아날로그 ODL 사용자 지정 특성의 사용자 지정 특성입니다.또한, 형식 라이브러리를 읽어 형식에 리플렉션을 사용 하 여 유사 합니다.

자세한 내용은 다음 항목을 참조하십시오.

Visual C++ 어셈블리 서명에 대 한 내용은 강력한 이름 어셈블리(어셈블리 서명)(C++/CLI).

yd21828z.collapse_all(ko-kr,VS.110).gif요구 사항

컴파일러 옵션:/clr

yd21828z.collapse_all(ko-kr,VS.110).gif예제

예제

다음 샘플에서는 사용자 지정 특성을 정의 하는 방법을 보여 줍니다.

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

예제

다음 예제에서는 사용자 지정 특성의 몇 가지 중요 한 기능을 보여 줍니다.예를 들어, 사용자 지정 특성의 일반적인 사용 예제: 클라이언트에 설명할 수 있습니다 완벽 하 게 자체 서버를 인스턴스화 합니다.

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

Output

  
  
  
  
  
  

예제

개체가 ^ 형식 variant 데이터 형식으로 바꿉니다.다음 예제에서는 개체의 배열을 사용 하는 사용자 지정 특성 정의 ^ 매개 변수로.

특성 인수가 컴파일 타임 상수 여야 합니다. 대부분의 경우에는 상수 리터럴 이어야 합니다.

typeid(C++ 구성 요소 확장) 사용자 지정 특성 블록에서 system:: 값을 반환 하는 방법에 대 한.

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

예제

런타임 사용자 지정 특성 클래스의 공용 부분이 serializable 해야 한다는 필요 합니다.사용자 지정 특성을 만들 때 사용자 지정 특성의 명명 된 인수가 컴파일 타임 상수로 제한 됩니다.(이것을 메타 데이터의 클래스 레이아웃에 추가 된 비트 시퀀스로 생각 하십시오.)

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

참고 항목

개념

런타임 플랫폼의 구성 요소 확장