Sdílet prostřednictvím


Cíle atributů (rozšíření komponent C++)

Atribut využití specifikátorů umožňují určit atribut cíle.Každý atribut je definován párovat určité prvky jazyka.Například atribut mohou být definovány možné párovat pouze třídy a struktur.Následující seznam obsahuje možné syntaktické prvky, které lze použít vlastní atribut .Kombinace těchto hodnot (pomocí logické nebo) mohou být použity.

Určete atributcílová databáze, předat jeden nebo více AttributeTargets do výčtu AttributeUsageAttribute při definování atribut.

Zde je seznam platný atribut cílů:

Cíl

Příklad použití

Všechna

(platí pro všechny konstrukce)

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

Sestavení

(platí pro sestavení jako celek)

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

Modul

(platí pro modul jako celek)

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

Třída

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

Struktura

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

výčet

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

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

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

Událost

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

Rozhraní

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

Delegát

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

atribut obvykle předchází přímo jazyk prvek, na který se vztahuje.V některých případech však umístění atribut není dostatečné pro stanovení atributurčený cílová databáze.Vezměme si jako příklad:

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

Syntakticky, je zjistit, pokud párovat metoda nebo metodavrácená hodnota je určen atribut (v tomto případě bude výchozí metoda).V takových případech mohou být použityvyužitíspecifikátor atribut. Například chcete-li atributpárovat vrácenou hodnotu, použijte returnvalue specifikátortakto:

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

Specifikátory využití atributu jsou požadovány v následujících situacích:

  • Určit modulnebo sestavení--úroveň atribut.

  • Určuje, že atribut platí pro vrácenou hodnotu metodanení 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
    
  • Určuje, že se vztahuje na přístupový objekt vlastnostuživatele, není vlastnost atribut :

    [method:MyAttr(123)] property int Property()  
    [property:MyAttr(123)] property int Property()
    [MyAttr(123)] property int get_MyPropy() // default: property
    
  • Určuje, že atribut platí pro přístupový objekt událost, nikoli událost:

    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
    }
    

specifikátor atributvyužitíplatí pouze pro atribut , který bezprostředně následuje; To znamená,

[returnvalue:Attr1, Attr2]

se liší od

[returnvalue:Attr1, returnvalue:Attr2]

Příklad

ms177223.collapse_all(cs-cz,VS.110).gifDescription

Tento příklad ukazuje, jak zadat více cílů.

ms177223.collapse_all(cs-cz,VS.110).gifKód

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

Viz také

Referenční dokumentace

Uživatelsky definované atributy (rozšíření komponent C++)