| title | Attributes (C++/CX) | Microsoft Docs |
|---|---|
| ms.custom | |
| ms.date | 12/30/2016 |
| ms.technology | cpp-windows |
| ms.reviewer | |
| ms.suite | |
| ms.tgt_pltfrm | |
| ms.topic | article |
| ms.assetid | 4438e03c-4de3-433d-abcc-31aa863bc0e0 |
| caps.latest.revision | 8 |
| author | ghogen |
| ms.author | ghogen |
| manager | ghogen |
An attribute is a special kind of ref class that can be prepended in square brackets to Windows Runtime types and methods to specify certain behaviors in metadata creation. Several predefined attributes—for example, Windows::Foundation::Metadata::WebHostHidden—are commonly used in C++/CX code. This example shows how the attribute is applied to a class:
[!code-cppcx_attributes#01]
You can also define custom attributes. Custom attributes must conform to these Windows Runtime rules:
-
Custom attributes can contain only public fields.
-
Custom attribute fields can be initialized when the attribute is applied to a class.
-
A field may be one of these types:
-
int32 (int)
-
uint32 (unsigned int)
-
bool
-
Platform::String^
-
Windows::Foundation::HResult
-
Platform::Type^
-
public enum class (includes user-defined enums)
-
The next example shows how to define a custom attribute and then initialize it when you use it.
[!code-cppcx_attributes#02]
Type System (C++/CX)
Visual C++ Language Reference
Namespaces Reference