共用方式為


屬性目標 (C++ 元件擴充功能)

屬性使用方式的規範,可讓您指定屬性目標。您可以定義每個屬性會套用到特定的語言項目。例如,可以定義屬性,只將套用至類別和結構。下列清單顯示可能的語法項目,以在其可以使用自訂屬性。這些值的組合 (使用邏輯或) 萬用字元。

若要指定屬性目標,將一或多AttributeTargets代表列舉器AttributeUsageAttribute定義屬性時。

以下是一份有效屬性目標:

目標

範例用法

全部

(適用於所有結構)

// 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; }
};

一般而言,屬性直接之前它所套用的語言項目。在某些情況下,不過,屬性的位置並不足以決定屬性的預期的目標。請考量以下範例:

[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]

範例

ms177223.collapse_all(zh-tw,VS.110).gif描述

這個範例會示範如何指定多個目標。

ms177223.collapse_all(zh-tw,VS.110).gif程式碼

// 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++ 元件擴充功能)