Tipos de Parâmetro de Atributo (Extensões de Componentes C++)

Valores passados aos atributos devem ser conhecidas para o compilador em tempo de compilar .Parâmetros do atributo podem ser dos seguintes tipos:

  • bool

  • char, unsigned char

  • short, unsigned short

  • int, unsigned int

  • long, unsigned long

  • __int64, unsigned __int64

  • float, double

  • wchar_t

  • char*or wchar_t* orSystem::String*

  • System::Type ^

  • System::Object ^

  • enum

Exemplo

ms177221.collapse_all(pt-br,VS.110).gifCódigo

// attribute_parameter_types.cpp
// compile with: /clr /c
using namespace System;
ref struct AStruct {};

[AttributeUsage(AttributeTargets::ReturnValue)]
ref struct Attr : public Attribute {
   Attr(AStruct ^ i){}
   Attr(bool i){}
   Attr(){}
};

ref struct MyStruct {
   static AStruct ^ x = gcnew AStruct;
   [returnvalue:Attr(x)] int Test() { return 0; }   // C3104
   [returnvalue:Attr] int Test2() { return 0; }   // OK
   [returnvalue:Attr(true)] int Test3() { return 0; }   // OK
};

Exemplo

ms177221.collapse_all(pt-br,VS.110).gifDescrição

Ao especificar atributos, todos os argumentos (posicionais) sem nome devem preceder quaisquer argumentos nomeados.

ms177221.collapse_all(pt-br,VS.110).gifCódigo

// extending_metadata_c.cpp
// compile with: /clr /c
using namespace System;
[AttributeUsage(AttributeTargets::Class)]
ref class MyAttr : public Attribute {
public:
   MyAttr() {}
   MyAttr(int i) {}
   property int Priority;
   property int Version;
};

[MyAttr] 
ref class ClassA {};   // No arguments

[MyAttr(Priority = 1)] 
ref class ClassB {};   // Named argument

[MyAttr(123)] 
ref class ClassC {};   // Positional argument

[MyAttr(123, Version = 1)] 
ref class ClassD {};   // Positional and named

Exemplo

ms177221.collapse_all(pt-br,VS.110).gifDescrição

Parâmetros do atributo podem ser matrizes unidimensionais dos tipos anteriores.

ms177221.collapse_all(pt-br,VS.110).gifCódigo

// extending_metadata_d.cpp
// compile with: /clr /c
using namespace System;

[AttributeUsage(AttributeTargets::Class)]
public ref struct ABC : public Attribute {
   ABC(array<int>^){}
   array<double> ^ param;
};

[ABC( gcnew array<int> {1,2,3}, param = gcnew array<double>{2.71, 3.14})] 
ref struct AStruct{};

Consulte também

Referência

Atributos Definidos pelo Usuário (Extensões de Componentes C++)