13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 24 Attributes24. Attributes[Note: Much of the <strong>C#</strong> language enables the programmer to specify declarative information about theentities defined in the program. For example, the accessibility of a method in a class is specified bydecorating it with the method-modifiers public, protected, internal, and private. end note]<strong>C#</strong> enables programmers to invent new kinds of declarative information, called attributes. Programmers canthen attach attributes to various program entities, and retrieve attribute information in a run-timeenvironment. [Note: For instance, a framework might define a HelpAttribute attribute that can be placedon certain program elements (such as classes and methods) to provide a mapping from those programelements to their documentation. end note]Attributes are defined through the declaration of attribute classes (§24.1), which may have positional andnamed parameters (§24.1.2). Attributes are attached to entities in a <strong>C#</strong> program using attribute specifications(§24.2), and can be retrieved at run-time as attribute instances (§24.3).24.1 Attribute classesA class that derives from the abstract class System.Attribute, whether directly or indirectly, is anattribute class. The declaration of an attribute class defines a new kind of attribute that can be placed on adeclaration. By convention, attribute classes are named with a suffix of Attribute. Uses of an attributemay either include or omit this suffix.24.1.1 Attribute usageThe attribute AttributeUsage (§24.4.1) is used to describe how an attribute class can be used.AttributeUsage has a positional parameter (§24.1.2) that enables an attribute class to specify the kinds ofdeclarations on which it can be used. [Example: The exampleusing System;[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]public class SimpleAttribute: Attribute{}defines an attribute class named SimpleAttribute that can be placed on class-declarations and interfacedeclarationsonly. The example[Simple] class Class1 {…}[Simple] interface Interface1 {…}shows several uses of the Simple attribute. Although this attribute is defined with the nameSimpleAttribute, when this attribute is used, the Attribute suffix may be omitted, resulting in theshort name Simple. Thus, the example above is semantically equivalent to the following[SimpleAttribute] class Class1 {…}[SimpleAttribute] interface Interface1 {…}end example]AttributeUsage has a named parameter (§24.1.2), called AllowMultiple, which indicates whether theattribute can be specified more than once for a given entity. If AllowMultiple for an attribute class is true,then that class is a multi-use attribute class, and can be specified more than once on an entity. IfAllowMultiple for an attribute class is false or it is unspecified, then that class is a single-use attributeclass, and can be specified at most once on an entity.[Example: The example305

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!