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) |
|
Assembly (si applica a un assembly del tutto) |
|
Modulo (si applica a un modulo complessivamente) |
|
Classe |
|
Struct |
|
enum |
|
Costruttore |
|
Metodo |
|
Proprietà |
|
Campo |
|
Evento |
|
Interfaccia |
|
Parametro |
|
Delegato |
|
ReturnValue |
|
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
Descrizione
In questo esempio viene illustrato come specificare le destinazioni multipli.
Codice
// 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++)