Udostępnij za pośrednictwem


Docelowe atrybuty (C++ Component Extensions)

Atrybut Użycie specyfikatorów pozwalają określić cele atrybut .Każdy atrybut jest zdefiniowany na stosuje się do niektórych elementów języka.Na przykład atrybut może być zdefiniowana stosuje się tylko do klas i strukturach.Na poniższej liście przedstawiono możliwe składni elementów, na których mogą być stosowane niestandardowego atrybut .Kombinacji tych wartości (przy użyciu logicznych lub) mogą być używane.

Aby określić atrybut target, aby przekazać jeden lub więcej AttributeTargets moduły wyliczające do AttributeUsageAttribute podczas definiowania atrybut.

Oto lista miejsc docelowych prawidłowy atrybut :

Docelowe

Przykładowe użycie

Wszystkie

(dotyczy wszystkich konstrukcje)

// attribute_targets_all.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::All)]
ref class Attr : public Attribute {};
[assembly:Attr];

Montaż

(stosuje się do zestaw , jako całość)

// attribute_targets_assembly.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Assembly)]
ref class Attr : public Attribute {};
[assembly:Attr];

Moduł

(stosuje się do moduł , jako całość)

// attribute_targets_module.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Module)]
ref class Attr : public Attribute {};
[module:Attr];

Klasa

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

Struct

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

Konstruktor

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

Metoda

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

Właściwość

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

Pole

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

Zdarzenia

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

Interfejs

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

Parametr

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

Pełnomocnik

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

atrybut bezpośrednio poprzedza zazwyczaj element języka, którego dotyczy.W niektórych przypadkach, stanowisko atrybut nie jest wystarczające do określenia atrybutdocelowej.Rozważmy następujący przykład:

[Attr] int MyFn(double x)...

Syntaktycznie, nie istnieje sposób stwierdzić, jeśli atrybut jest przeznaczony do zastosowania metoda lub metodazwracana wartość (w tym przypadku domyślnie metoda).W takich przypadkach dopuszcza się użycie atrybut specyfikator .Na przykład, aby atrybut stosuje się do wartości zwracanej, należy użyć returnvalue specyfikator, w następujący sposób:

[returnvalue:Attr] int MyFn(double x)... // applies to return value

Specyfikatory użycie atrybutu są wymagane w następujących sytuacjach:

  • Aby określić modułlub zestaw--poziom atrybut.

  • Aby określić, że atrybut stosuje się do metodawartości zwracanej, nie metoda:

    [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
    
  • Aby określić, że atrybut jest stosowana do akcesor właściwość, nie właściwość:

    [method:MyAttr(123)] property int Property()  
    [property:MyAttr(123)] property int Property()
    [MyAttr(123)] property int get_MyPropy() // default: property
    
  • Aby określić, że atrybut stosuje się do zdarzenieakcesor, nie zdarzenie:

    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
    }
    

Użycie atrybut specyfikator dotyczy tylko atrybut , który następuje bezpośrednio oznacza to,

[returnvalue:Attr1, Attr2]

różni się od

[returnvalue:Attr1, returnvalue:Attr2]

Przykład

ms177223.collapse_all(pl-pl,VS.110).gifOpis

Próbka ta pokazuje, jak określić wiele elementów docelowych.

ms177223.collapse_all(pl-pl,VS.110).gifKod

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

Zobacz też

Informacje

Atrybuty zdefiniowane przez użytkownika (C++ Component Extensions)