// attribute_targets_all.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::All)]
ref class Attr : public Attribute {};
[assembly:Attr];
Assembly
(整體方式套用至組譯碼)
// 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]
};
事件
// 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);
};
委派
// 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; }
};
[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