次の方法で共有


属性の対象 (C++ コンポーネント拡張)

属性の用途の指定子は、属性ターゲットを指定することを許可します。各属性は、特定の言語要素に適用するために定義されます。たとえば、属性は、クラスや構造体にのみ適用するために定義されることがあります。次の一覧は、カスタム属性が使用できる構文要素を示します。これらの値の組み合わせは論理的を使用して、または)を使用する場合があります。

属性を定義するときの属性ターゲットを AttributeUsageAttributeAttributeTargets の一つ以上の列挙子を渡すように指定する場合は。

次は、有効な属性ターゲットの一覧です:

Target

サンプルの使用方法

すべて

(すべての構造体に適用されます)

// attribute_targets_all.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::All)]
ref class Attr : public Attribute {};
[assembly:Attr];

Assembly

(全体としてアセンブリに適用されます)

// attribute_targets_assembly.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Assembly)]
ref class Attr : public Attribute {};
[assembly:Attr];

Module

(モジュール全体に対してに適用されます)

// attribute_targets_module.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Module)]
ref class Attr : public Attribute {};
[module:Attr];

Class

// attribute_targets_class.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Class)]
ref class Attr : public System::Attribute {};
[Attr]   // same as [class:Attr]
ref class MyClass {};

構造体

// attribute_targets_struct.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Struct)]
ref class Attr : public Attribute {};
[Attr]   // same as [struct:Attr]
value struct MyStruct{};

enum

// attribute_targets_enum.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Enum)]
ref class Attr : public Attribute {};
[Attr]   // same as [enum:Attr]
enum struct MyEnum{e, d};

コンストラクター

// attribute_targets_constructor.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Constructor)]
ref class Attr : public Attribute {};
ref struct MyStruct{
   [Attr] MyStruct(){}   // same as [constructor:Attr]
};

メソッド

// attribute_targets_method.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Method)]
ref class Attr : public Attribute {};
ref struct MyStruct{
   [Attr] void Test(){}   // same as [method:Attr]
};

プロパティ

// attribute_targets_property.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Property)]
ref class Attr : public Attribute {};
ref struct MyStruct{
   [Attr] property int Test;   // same as [property:Attr]
};

Field

// attribute_targets_field.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Field)]
ref class Attr : public Attribute {};
ref struct MyStruct{
   [Attr] int Test;   // same as [field:Attr]
};

Event

// attribute_targets_event.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Event)]
ref class Attr : public Attribute {};
delegate void ClickEventHandler(int, double);
ref struct MyStruct{
   [Attr] event ClickEventHandler^ OnClick;   // same as [event:Attr]
};

Interface

// attribute_targets_interface.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Interface)]
ref class Attr : public Attribute {};
[Attr]   // same as [event:Attr]
interface struct MyStruct{};

パラメーター

// attribute_targets_parameter.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Parameter)]
ref class Attr : public Attribute {};
ref struct MyStruct{
   void Test([Attr] int i);
   void Test2([parameter:Attr] int i);
};

Delegate

// attribute_targets_delegate.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Delegate)]
ref class Attr : public Attribute {};
[Attr] delegate void Test();
[delegate:Attr] delegate void Test2();

ReturnValue

// attribute_targets_returnvalue.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::ReturnValue)]
ref class Attr : public Attribute {};
ref struct MyStruct {
   // Note required specifier
   [returnvalue:Attr] int Test() { return 0; }
};

通常、属性は直接適用する言語要素を記述します。場合によっては、属性の位置は属性の目的のターゲットを決定するわけではありません。次の例について考えます。

[Attr] int MyFn(double x)...

構文上、属性をメソッドまたはメソッドの戻り値に適用するかどうかを示す方法がありません。この場合、メソッドで設定します)。このような場合は、属性の用途の指定子が使用される場合があります。たとえば次のように、属性を戻り値に適用されます returnvalue の指定子の使用:

[returnvalue:Attr] int MyFn(double x)... // applies to return value

属性の用途の指定子は、次の場合に必要です:

  • アセンブリまたはモジュール レベルの属性を指定します。

  • 属性は、メソッドの戻り値に適用することを指定するには、メソッド:

    [method:Attr] int MyFn(double x)...     // Attr applies to method
    [returnvalue:Attr] int MyFn(double x)...// Attr applies to return value
    [Attr] int MyFn(double x)...            // default: method
    
  • 属性がプロパティのアクセサーに適用することを指定するには、プロパティ:

    [method:MyAttr(123)] property int Property()  
    [property:MyAttr(123)] property int Property()
    [MyAttr(123)] property int get_MyPropy() // default: property
    
  • 属性がイベントのアクセサーに適用することを指定するには、イベント:

    delegate void MyDel();
    ref struct X {
       [field:MyAttr(123)] event MyDel* MyEvent;   //field
       [event:MyAttr(123)] event MyDel* MyEvent;   //event
       [MyAttr(123)] event MyDel* MyEvent;   // default: event
    }
    

属性の用途の指定子は、その後に続く属性にだけ適用します; つまり、

[returnvalue:Attr1, Attr2]

とは異なります。

[returnvalue:Attr1, returnvalue:Attr2]

ms177223.collapse_all(ja-jp,VS.110).gifDescription

このサンプルは、複数のターゲットを指定する方法を示します。

ms177223.collapse_all(ja-jp,VS.110).gifコード

// attribute_targets.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Class | AttributeTargets::Struct, AllowMultiple = true )]
ref struct Attr : public Attribute {
   Attr(bool i){}
   Attr(){}
};

[Attr]
ref class MyClass {};

[Attr]
[Attr(true)]
value struct MyStruct {};

参照

関連項目

ユーザー定義の属性 (C++ コンポーネント拡張)