Information Technology Reference
In-Depth Information
What Is an Attribute?
C# is an imperative language , which loosely means that you write instructions that tell the pro-
gram what to do and how to do it. But it also has aspects of declarative programming , which
means that you declare the characteristics of a component, rather than producing a detailed
algorithm to implement those characteristics.
A good example of the declarative aspect of the language is the member access modifiers.
You can apply the modifiers public , private , protected , and so forth on a member to declare its
level of access. The compiler stores this information in the assembly, and the compiler and the
CLR take care of the details.
Besides these declarative aspects of the language itself, C# also allows you to store user-
defined declarative information about the constructs of a program's source code, in the
program's assembly. You can do this by using attributes . The information stored is not data
used by the program—it is data about the program and is called metadata .
An attribute is a special type of class, designed specifically for storing information about
the program constructs. You can apply attributes to a construct of a program's source code, to
declare something about the construct.
￿
The program construct to which you apply an attribute is called its target .
￿
Programs designed to retrieve and use the metadata, are said to be consumers of the
attributes.
Figure 21-1 is an overview of the components involved in using attributes, and illustrates
the following points about attributes:
￿You apply attributes to program constructs in the source code.
￿
The compiler takes the source code and produces metadata from the attributes, and
places that metadata in the assembly.
￿
Other programs, called consumers , can access the metadata of the attributes along with
the metadata for the rest of the components of the program. Notice that the compiler
both produces the assembly and is also a consumer of attributes.
Figure 21-1. The components involved with creating and using attributes
Search WWH ::




Custom Search