Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

attribute

Allows you to create a custom attribute.

[ attribute(
   AllowOn,
   AllowMultiple=boolean,
   Inherited=boolean
) ]

Parameters

AllowOn
Specifies the language elements to which the custom attribute can be applied. Default is System::AttributeTargets::All (see System::AttributeTargets).
AllowMultiple
Specifies whether the custom attribute can be applied repeatedly to a construct. Default is FALSE.
Inherited
Indicates if the attribute is to be inherited by subclasses. The compiler provides no special support for this functionality; it is the job of the attribute consumers (Reflection, for example) to respect this information. If Inherited is TRUE, the attribute is inherited. If AllowMultiple is TRUE, the attribute will accumulate on the derived member; if AllowMultiple is FALSE, the attribute will override (or replace) in inheritance. If Inherited is FALSE, the attribute will not be inherited. Default is TRUE.

Attribute Context

Applies to __gc class, __gc struct
Repeatable No
Required attributes None
Invalid attributes None

For more information about the attribute contexts, see Attribute Contexts.

Remarks

You define a custom attribute by placing the attribute attribute on a managed class or struct definition. The name of the class is the custom attribute. For example:

[ attribute(Parameter) ]
public __gc class MyAttr {...};

defines an attribute called MyAttr that can be applied to function parameters. The class must be public if the attribute is going to be used in other assemblies.

Note   To prevent namespace collisions, all attribute names implicitly end with "Attribute"; in this example, the name of the attribute and class is actually MyAttrAttribute, but MyAttr and MyAttrAttribute can be used interchangeably.

The class's public constructors define the attribute's unnamed parameters. Overloaded constructors allow multiple ways of specifying the attribute, so a custom attribute that is defined the following way:

[ attribute(Class) ] // Defines attribute that applies to classes
public __gc class MyAttr {
public:
    MyAttr() {...}         // Constructor with no parameters
    MyAttr(int arg1) {...} // Constructor with one parameter
};

can be applied in the following way:

[MyAttr] __gc class ClassA {...};      // Attribute with no parameters
[MyAttr(123)] __gc class ClassB {...}; // Attribute with one parameter

The class's public data members and properties are the attribute's optional named parameters:

[ attribute(Class) ]
__gc class MyAttr {
public:
// Property Priority becomes attribute's named parameter Priority
    __property void set_Priority(int value) {...}
    __property int get_Priority() {...}
// Data member Version becomes attribute's named parameter Version
    int Version;
    MyAttr() {...}         // constructor with no parameters
    MyAttr(int arg1) {...} // constructor with one parameter
};

This attribute could be applied as follows:

[MyAttr(123,Version=2)] __gc class ClassC {...};

For a list of possible attribute parameter types, see Custom Attributes.

The attribute attribute has a positional parameter that specifies the language elements to which the custom attribute may be applied. This value is a set of System::AttributeTargets enums combined together using logical OR. For example, you can define a custom attribute that applies only to classes and structs.

[ attribute(Class|Struct) ]
__gc class MyAttr {...}; // MyAttr applies to classes and structs
Note   The struct attribute target means that the attribute can be applied on value types.

The following is a list of the System::AttributeTargets enums:

  • Assembly
  • Module
  • Class
  • Struct
  • Enum
  • Constructor
  • Method
  • Property
  • Field
  • Event
  • Interface
  • Parameter
  • Delegate
  • ReturnValue
  • All   Defines an attribute that applies to all the previous items.

The default value is AttributeTargets::All.

The attribute attribute has an AllowMultiple parameter that specifies whether the custom attribute is single use or multiuse (can appear more than once on the same entity).

[ attribute(Class, AllowMultiple = true) ]
__gc class MyAttr {...}; // MyAttr is a multiuse attribute
// Example of use:
[MyAttr(123),MyAttr(124)] ClassA {...};

Custom attribute classes are derived directly or indirectly from System::Attribute, which makes identifying attribute definitions in metadata fast and easy. The attribute attribute implies inheritance from System::Attribute, so explicit derivation is not necessary:

[ attribute(Class) ]
__gc class MyAttr

is equivalent to

[ attribute(Class) ]
__gc class MyAttr : System::Attribute // OK, but redundant.

attribute is an alias for System::AttributeUsageAttribute (not AttributeAttribute; this is an exception to the attribute naming rule).

Example

// cpp_attr_ref_attribute.cpp
// compile with: /LD /clr
#using "mscorlib.dll"
using namespace System;
[attribute(Class)]
__gc class ABC
{
public:
   ABC(System::Type*) {}
};

[ABC(__typeof(String))]   // __typeof operator yields System::Type*
__gc class MyClass
{
};

See Also

Attributes Alphabetical Reference | Custom Attributes

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.