Attributziele (Komponentenerweiterungen für C++)

Attributverwendungs Parameterbezeichner Attribut können Sie Ziele angeben.Jedes Attribut definiert wird, die auf spezifische Sprachelemente angewendet werden sollen.Beispielsweise würde ein Attribut definiert werden, dass sie nur für Klassen und Strukturen angewendet werden sollen.In der folgenden Liste sind die möglichen syntaktischen Elemente an, bei denen ein benutzerdefiniertes Attribut verwendet werden kann.Kombinationen dieser Werte (mit dem logischen oder) verwendet werden.

So Attributziel angegeben werden, um eine oder mehrere AttributeTargets Enumeratoren auf AttributeUsageAttribute zu übergeben, wenn das Attribut definiert ist.

Im Folgenden finden Sie eine Liste der gültigen Attribut Ziele:

Target

Verwendungsbeispiel

Alle

(gilt für alle Konstrukte)

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

Assembly

(gilt für eine Assembly als Ganzes)

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

Modul

(gilt für ein Modul als Ganzes)

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

Klasse

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

Struktur

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

Konstruktor

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

Methode

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

Eigenschaft

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

Feld

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

Ereignis

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

Schnittstelle

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

Parameter

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

Delegat

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

In der Regel wird ein Attribut direkt auf das Sprachelement voran, auf das es angewendet wird.In einigen Fällen jedoch ist die Position eines Attributs nicht ausreichend, um das beabsichtigte Ziel des Attributs zu bestimmen.Betrachten Sie das folgende Beispiel:

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

Syntaktisch zu kommunizieren, ist es nicht möglich, wenn das Attribut bestimmt ist, an die Methode oder den Rückgabewert der Methode angewendet wird (in diesem Fall übergibt dieses an die Methode.)In solchen Fällen wird ein Attributverwendungs Formatbezeichner verwenden kann.Um beispielsweise das Attribut auf den Rückgabewert angewendet werden soll, verwenden Sie den returnvalue Bezeichner wie folgt:

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

Attributverwendungs Parameterbezeichner sind in den folgenden Fällen erforderlich:

  • So erstellen Sie eine Erfassen oder auf Modulebene ein Attribut angeben.

  • So geben Sie an, dass ein Attribut auf den Rückgabewert einer Methode gilt, jedoch nicht die Methode:

    [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
    
  • So geben Sie an, dass ein Attribut auf den Accessor einer Eigenschaft die Eigenschaft nicht angewendet werden kann:

    [method:MyAttr(123)] property int Property()  
    [property:MyAttr(123)] property int Property()
    [MyAttr(123)] property int get_MyPropy() // default: property
    
  • So geben Sie an, dass ein Attribut auf den Accessor eines Ereignisses, gilt das Ereignis nicht:

    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
    }
    

Ein Attributverwendungs Parameterbezeichner gilt nur für das Attribut zu, das ihm unmittelbar folgt. d. h.

[returnvalue:Attr1, Attr2]

ist unterschiedlich

[returnvalue:Attr1, returnvalue:Attr2]

Beispiel

ms177223.collapse_all(de-de,VS.110).gifBeschreibung

In diesem Beispiel wird gezeigt, wie mehrere Ziele angibt.

ms177223.collapse_all(de-de,VS.110).gifCode

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

Siehe auch

Referenz

Benutzerdefinierte Attribute (Komponentenerweiterungen für C++)