Destinazioni degli attributi (Estensioni del componente C++)

Gli identificatori di utilizzo degli attributi consentono di specificare le destinazioni dell' attributo.Ogni attributo viene definito applicare a determinati elementi del linguaggio.Ad esempio, un attributo potrebbe essere definito essere applicato solo alle classi e gli struct.In l ' elenco seguente vengono illustrati gli elementi sintattici possibili in cui un attributo personalizzato può essere utilizzato.Le combinazioni di questi valori mediante logico o) possono essere utilizzate.

Per specificare destinazione dell' attributo, per passare uno o più enumeratori di AttributeTargets a AttributeUsageAttribute quando si definiscono l'attributo.

Di seguito viene riportato un elenco di destinazioni degli attributi validi:

Destinazione

Utilizzo dell'esempio

Tutte

(si applica a tutti i costrutti)

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

Assembly

(si applica a un assembly del tutto)

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

Modulo

(si applica a un modulo complessivamente)

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

Classe

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

Struct

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

Costruttore

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

Metodo

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

Proprietà

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

Campo

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

Evento

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

Interfaccia

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

Parametro

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

Delegato

// 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 genere, un attributo direttamente precede l'elemento del linguaggio in cui si applica.In alcuni casi, tuttavia, la posizione di un attributo non è sufficiente per determinare il database di destinazione previsto dell' attributo.Si consideri l'esempio seguente:

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

Sintassi, non è possibile stabilire se l'attributo deve essere applicato al metodo o al valore restituito del metodo (in questo caso, viene impostato come valore predefinito al metodo).In tali casi, un identificatore di utilizzo di un attributo può essere utilizzato.Ad esempio, per fare l'attributo applicare al valore restituito, utilizzare l'identificatore di returnvalue , come segue:

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

Gli identificatori di utilizzo di un attributo sono obbligatori nelle seguenti situazioni:

  • Per specificare un attributo a livello di modulo o dell' assembly.

  • Per specificare che un attributo viene applicato al valore restituito di un metodo, non il metodo:

    [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
    
  • Per specificare che un attributo viene applicato alla funzione di accesso di una proprietà, non la proprietà:

    [method:MyAttr(123)] property int Property()  
    [property:MyAttr(123)] property int Property()
    [MyAttr(123)] property int get_MyPropy() // default: property
    
  • Per specificare che un attributo viene applicato alla funzione di accesso di un evento, non l'evento:

    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
    }
    

Un identificatore di utilizzo di un attributo si applica solo all' attributo immediatamente successivo; ovvero

[returnvalue:Attr1, Attr2]

è diverso da

[returnvalue:Attr1, returnvalue:Attr2]

Esempio

ms177223.collapse_all(it-it,VS.110).gifDescrizione

In questo esempio viene illustrato come specificare le destinazioni multipli.

ms177223.collapse_all(it-it,VS.110).gifCodice

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

Vedere anche

Riferimenti

Attributi definiti dall'utente (Estensioni del componente C++)