Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Attribute usage specifiers let you specify attribute targets. Each attribute is defined to apply to certain language elements. For example, an attribute might be defined to apply only to classes and structs. The following list shows the possible syntactic elements on which a custom attribute can be used. Combinations of these values (using logical or) may be used.
To specify attribute target, to pass one or more AttributeTargets enumerators to AttributeUsageAttribute when defining the attribute.
The following is a list of the valid attribute targets:
Target |
Sample Usage |
---|---|
All (applies to all constructs) |
|
Assembly (applies to an assembly as a whole) |
|
Module (applies to a module as a whole) |
|
Class |
|
Struct |
|
enum |
|
Constructor |
|
Method |
|
Property |
|
Field |
|
Event |
|
Interface |
|
Parameter |
|
Delegate |
|
ReturnValue |
|
Typically, an attribute directly precedes the language element to which it applies. In some cases, however, the position of an attribute is not sufficient to determine the attribute's intended target. Consider this example:
[Attr] int MyFn(double x)...
Syntactically, there is no way to tell if the attribute is intended to apply to the method or to the method's return value (in this case, it defaults to the method). In such cases, an attribute usage specifier may be used. For example, to make the attribute apply to the return value, use the returnvalue specifier, as follows:
[returnvalue:Attr] int MyFn(double x)... // applies to return value
Attribute usage specifiers are required in the following situations:
To specify an assembly- or module-level attribute.
To specify that an attribute applies to a method's return value, not the method:
[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
To specify that an attribute applies to a property's accessor, not the property:
[method:MyAttr(123)] property int Property() [property:MyAttr(123)] property int Property() [MyAttr(123)] property int get_MyPropy() // default: property
To specify that an attribute applies to an event's accessor, not the event:
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 }
An attribute usage specifier applies only to the attribute that immediately follows it; that is,
[returnvalue:Attr1, Attr2]
is different from
[returnvalue:Attr1, returnvalue:Attr2]
Example
Description
This sample shows how to specify multiple targets.
Code
// 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 {};