特性用法说明符允许您指定特性目标。每个特性定义适用于某些语言元素。例如,属性来定义仅适用于类和结构。下面的列表显示自定义特性可可以使用的语法元素的。可以使用这些值的组合 (使用逻辑或)。
指定特性目标,将一个或多 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]
};
|
属性 |
// 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{};
|
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);
};
|
委托 |
// 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; }
};
|
|
|
通常,属性直接位于其应用的语言元素。但是,在某些情况下,特性的位置不足以确定属性的预期目标。请看以下示例:
[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++ 组件扩展)