Information Technology Reference
In-Depth Information
Applying an Attribute
The purpose of an attribute is to tell the compiler to emit a certain set of metadata about a pro-
gram construct, to the assembly. You do this by
applying
the attribute to the construct.
You apply an attribute by placing an
attribute section
immediately before the construct.
An
attribute section
consists of square brackets enclosing an attribute name and some-
times a parameter list.
For example, the following code shows the headings of two classes. The first few lines of
code show an attribute named
Serializable
applied to a class called
MyClass
. Notice that
Serializable
has no parameter list. The second class declaration has an attribute called
MyAttribute
, which has a parameter list with two
string
parameters.
[ Serializable ] // Attribute
public class MyClass
{ ...
[ MyAttribute("Simple class", "Version 3.57") ] // Attribute with parameters
public class MyOtherClass
{ ...
Some important things to know about attributes are the following:
Most attributes apply only to the construct immediately following the attribute section
or sections.
A construct with an attribute applied to it is sometimes said to be
decorated
, or
adorned
,
with the attribute. Both terms are common.