An attribute is a declarative tag that is used to convey information to runtime about the behaviors of various elements like classes, methods, structures, enumerators, assemblies etc., in your program. You can add declarative information to a program by using an attribute. A declarative tag is depicted by square ([ ]) brackets placed above the element it is used for.
Attributes are used for adding metadata, such as compiler instruction and other information such as comments, description, methods and classes to a program. The .Net Framework provides two types of attributes: the pre-defined attributes and custom built attributes.
After attributes have been associated with program elements, reflection can be used to query their existence and values. In the .NET Framework version 1.0 and 1.1, custom attributes are examined in the execution context. The .NET Framework version 2.0 provides a new load context, the reflection-only context, which can be used to examine code that cannot be loaded for execution.
Attributing is attaching declarative information to various programming entities and retrieving this at run-time. A program can specify the accessibility of a method in a class by specifying it with some access modifiers like private, public etc.
Where as the declarative information can be attached by defining and using attributes.
Custom attributing is nothing but a way to find out new attributes which is very requirement specific.
Introduction
Attributes give you the ability to store additional information with your application or assembly metadata, which can be queried at runtime by using the .NET Frameworks Reflection capabilities. For example, attributes could be used to indicate whether a class is serializable, or which field in a database a particular property should be written to. In this article we will create a simple custom attribute, and demonstrate how easy it is to retrieve this information at run-time.
Creating our Custom Attribute class
To create our custom attribute class, we will descend from the System.Attribute class as follows :-
public class DescriptionAttribute: Attribute {
private string description;
public string Description {get {return description;}}
public DescriptionAttribute(string description) {
this.description = description;
}
}
There is nothing too complicated about our attribute class. It simply allows for a description string to be associated with it (passed into the constructor), and exposes this as a property which can be queried at runtime.
Using our Custom Attribute
We will start off by creating a test class, and use our attribute to associate a description with this class. To do this, we use the following syntax :-
[DescriptionAttribute("This is our test class")]
class AttributeClass {}
or
[Description("This is our test class")]
class AttributeClass {}
Both methods are identical in functionality, and are both valid syntax. The compiler will initially attempt to locate an attribute using the specified class name. If it is unsuccessful, the compiler will append 'Attribute' to the end of the class name and search again. As it is common practice to declare attribute classes using the Attribute suffix but reference them without it, this is the approach I will be taking for the rest of this article.
Querying for our Custom Attribute
Once we have associated our attribute with various source code elements, we can query the metadata of these elements at run-time by using the .NET Framework Reflection classes. Reflection can be used to gain information about every aspect of our class or assembly, including the class or assembly itself.
Metadata. Data about your objects/methods/properties.
For example I might declare an Attribute called: DisplayOrder so I can easily control in what order properties should appear in the UI. I could then append it to a class and write some GUI components that extract the attributes and order the UI elements appropriately.
No comments:
Post a Comment