Attributes
Types, members, and other entities in a C# program support modifiers that control certain aspects of their behavior. For example, the accessibility of a method is controlled using the public, protected, internal, and private modifiers. C# generalizes this capability such that user-defined types of declarative information can be attached to program entities and retrieved at run-time. Programs specify this additional declarative information by defining and using attributes. The following example declares a HelpAttribute attribute that can be placed on program entities to provide links to their associated documentation. using System; public class HelpAttribute: Attribute public HelpAttribute(string url) { public string Url { public string Topic { All attribute classes derive from the System.Attribute base class provided by the.NET Framework. Attributes can be applied by giving their name, along with any arguments, inside square brackets just before the associated declaration. If an attribute’s name ends in Attribute, that part of the name can be omitted when the attribute is referenced. For example, the HelpAttribute attribute can be used as follows. [Help("http://msdn.microsoft.com/.../MyClass.htm")] This example attaches a HelpAttribute to the Widget class and another HelpAttribute to the Display method in the class. The public constructors of an attribute class control the information that must be provided when the attribute is attached to a program entity. Additional information can be provided by referencing public read-write properties of the attribute class (such as the reference to the Topic property previously). The following example shows how attribute information for a given program entity can be retrieved at run-time using reflection. using System; class Test static void Main() { When a particular attribute is requested through reflection, the constructor for the attribute class is invoked with the information provided in the program source, and the resulting attribute instance is returned. If additional information was provided through properties, those properties are set to the given values before the attribute instance is returned.
|