Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
617 24 Attributes [Note: Much of the C# language enables the programmer to specify declarative information about the entities defined in the program. For example, the accessibility of a method in a class is spe- cified by decorating it with the method-modifiers public, protected, internal, and private. end note] C# enables programmers to invent new kinds of declarative information, called attributes. Pro- grammers can then attach attributes to various program entities, and retrieve attribute informa- tion in a run-time environment. [Note: For instance, a framework might define a HelpAttribute attribute that can be placed on certain program entities (such as classes and methods) to provide a mapping from those program entities to their documentation. end note] Attributes are defined through the declaration of attribute classes (§24.1), which can have posi- tional and named parameters (§24.1.2). Attributes are associated with program entities using attribute specifications (§24.2), and can be retrieved at run-time as attribute instances (§24.3). 24.1 Attribute classes A class that derives from the abstract class System.Attribute, whether directly or indirectly, is an attribute class. The declaration of an attribute class defines a new kind of attribute that can be placed on program entities. By convention, attribute classes are named with a suffix of Attri- bute. Uses of an attribute can either include or omit this suffix. Suffix omission clarification Uses of an attribute class as an attribute specification can either include or omit this suffix. However, in all other cases where the attribute class is referenced, the suffix must be included as it is part of the name. For example, consider the code fragment: