Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

C++/CLI: Attributes


Rex Jaeschke is an independent consultant, author, and seminar leader. He serves as editor of the standards for C++/CLI, CLI, and C#. Rex can be reached at http://www.RexJaeschke.com.


The runtime environment permits keyword-like descriptive declarations, called attributes, to be present on program elements such as types, fields, functions, and properties. Attribute information is stored in the metadata output by a compiler. As such, it can be used to describe the code to the runtime, or to modify somehow behavior at run time.

In this article, we'll see some of the attributes provided by the .NET Framework, and we'll look at several in detail.

Introduction

Depending on how it was defined, an attribute can be applied to one or more program elements. The general form of an attribute is as follows:

[assembly:Attrib1( ... )]

[Attrib2( ... )]
public ref class C
{
        [Attrib3( ... )]
        member declaration
}

In C++/CLI, an attribute is specified inside a pair of matching brackets ([]), as shown above. An attribute can be attached to the assembly as a whole (as with Attrib1), to a type (as with Attrib2), or to a member of a type (as with Attrib3). It can even be attached to a particular parameter in a function declaration, or to the return type of a function (neither of which is shown here).

In previous installments, we saw the use of a number of attributes provided by the .NET Framework; they are: Flags, ThreadStatic, Serializable, and NonSerialized.

The full name of an attribute class contains the suffix Attribute; for example, the full name of the attribute ThreadStatic is really ThreadStaticAttribute; however, the suffix can be omitted, and it has been throughout this text.

When an attribute is used, a call to its constructor is placed within the brackets. To see what arguments can be passed to that constructor, consult the on-line help for the given attribute. Apart from having one or more fixed arguments, some arguments can be passed by name rather than by position. For example,

[DllImport("MyLib.DLL", EntryPoint = "MyFunction", SetLastError = true,
CharSet = CharSet::Unicode, ExactSpelling = true,
CallingConvention = CallingConvention::StdCall)]

In such cases, the ordering of the named arguments is irrelevant.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.