Windows Dev Center

Attributes (C++/CX)

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:

public ref class MyClass : Windows::UI::Xaml::DependencyObject {};

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.

    public ref class MyCustomAttribute sealed : Platform::Metadata::Attribute {
		int Num;
		Platform::String^ Msg;		

    [MyCustomAttribute(Num=5, Msg="Hello")]
    public ref class Class1 sealed
© 2015 Microsoft