Skip to content

Latest commit

 

History

History
121 lines (85 loc) · 2.58 KB

attribute-parameter-types-cpp-component-extensions.md

File metadata and controls

121 lines (85 loc) · 2.58 KB
descriptiontitlems.datems.topichelpviewer_keywordsms.assetid
Learn more about: Attribute Parameter Types (C++/CLI and C++/CX)
Attribute Parameter Types (C++/CLI and C++/CX)
10/12/2018
reference
custom attributes, parameter types
d9f127a3-7f08-456f-acc6-256805632712

Attribute Parameter Types (C++/CLI and C++/CX)

Values passed to attributes must be known to the compiler at compile time. Attribute parameters can be of the following types:

  • bool

  • char, unsigned char

  • short, unsigned short

  • int, unsigned int

  • long, unsigned long

  • __int64, unsigned __int64

  • float, double

  • wchar_t

  • char* or wchar_t* or System::String*

  • System::Type ^

  • System::Object ^

  • enum

Example: Attribute parameter types

Code

// attribute_parameter_types.cpp// compile with: /clr /cusingnamespaceSystem; ref structAStruct {}; [AttributeUsage(AttributeTargets::ReturnValue)] ref structAttr : publicAttribute { Attr(AStruct ^ i){} Attr(bool i){} Attr(){} }; ref structMyStruct { static AStruct ^ x = gcnew AStruct; [returnvalue:Attr(x)] intTest() { return0; } // C3104 [returnvalue:Attr] intTest2() { return0; } // OK [returnvalue:Attr(true)] intTest3() { return0; } // OK };

Example: Unnamed arguments precede named arguments

Description

When specifying attributes, all unnamed (positional) arguments must precede any named arguments.

Code

// extending_metadata_c.cpp// compile with: /clr /cusingnamespaceSystem; [AttributeUsage(AttributeTargets::Class)] ref classMyAttr : publicAttribute { public:MyAttr() {} MyAttr(int i) {} property int Priority; property int Version; }; [MyAttr] ref classClassA {}; // No arguments [MyAttr(Priority = 1)] ref classClassB {}; // Named argument [MyAttr(123)] ref classClassC {}; // Positional argument [MyAttr(123, Version = 1)] ref classClassD {}; // Positional and named

Example: One-dimensional array attribute parameter

Description

Attribute parameters can be one-dimensional arrays of the previous types.

Code

// extending_metadata_d.cpp// compile with: /clr /cusingnamespaceSystem; [AttributeUsage(AttributeTargets::Class)] public ref structABC : publicAttribute { ABC(array<int>^){} array<double> ^ param; }; [ABC( gcnew array<int> {1,2,3}, param = gcnew array<double>{2.71, 3.14})] ref structAStruct{};

See also

User-Defined Attributes

close