特性用法说明符允许指定特性目标。每个特性定义适用于特定的语言元素。 例如,某个特征可能定义仅适用于类和结构。下面的列表演示自定义属性可以使用的语法元素。 可以使用这些值 (使用逻辑或)。
指定特性目标,将一个或多个 AttributeTargets 枚举数为 AttributeUsageAttribute,在定义特性时。
下面是有效的特性目标的列表:
全部
(适用于所有构造) |
// attribute_targets_all.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::All)]
ref class Attr : public Attribute {};
[assembly:Attr];
|
程序集
(应用于整个程序集) |
// attribute_targets_assembly.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Assembly)]
ref class Attr : public Attribute {};
[assembly:Attr];
|
模块
(应用于整个模块) |
// attribute_targets_module.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Module)]
ref class Attr : public Attribute {};
[module:Attr];
|
类 |
// 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]
};
|
Property |
// 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]
};
|
字段 |
// 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]
};
|
接口 |
// 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();
|
返回值 |
// 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]
示例
说明
此示例演示如何指定多个目标。
代码
// 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++ 组件扩展)