Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » .NET Framework »
Attributes : The Additional Information that is associated with a class, structure, method...
|
Attributes are specified within square brackets and they precede the item to which they apply.
Basics of an Attribute All attribute classes must be subclasses of Attribute. When an attribute class is declared, it is preceded by an attribute called AttributeUsage (Refer the article regarding: “built –in attributes”). Built-in attribute’s indicate the types of items to which the attribute can be applied.
Creating an Attribute Inside an attribute class, you have to define the members that support the attribute. For example,
[AttributeUsage( AttributeTargets.All )]
public class OneAttribute : Attribute
{
string data;
public OneAttribute( string comment )
{
data = comment ;
}
public string remarks
{
get
{
return data;
}
}
}
The name of this attribute is OneAttribute and its declaration is preceded by the AttributeUsage attribute, which specifies that OneAttribute can be applied to all types of items. Using AttributeUsage, it is possible to narrow the list of items to which an attribute can be attached. [its capabilities are discussed in the article titled: “built-in attributes”].
Next, OneAttribute is declared and it inherits Attribute. Inside OneAttribute there is one private field, data, that supports one public, read-only property: remarks. This property holds the description that will be associated with the attribute. There is one public constructor that takes a string argument and assigns it to remarks..
Attaching an Attribute After the Attribute class is defined, you can attach the attribute to an item. An attribute precedes the item to which it is attached and is specified by enclosing its constructor inside square-brackets. For example here is how OneAtribute can be associated with a class:
[OneAttribute( "Here's a class that uses an attribute." )]
class GetAttribute
{
//...
}
This constructs a OneAttribute that contains the comment “Here's a class that uses an attribute." This attribute is then associated with GetAttribute.
When attaching an attribute, it’s not compulsory to specify the Attribute suffix. For example, the preceding class could be declared this way:
[One( "Here's a class that uses an attribute." )] class GetAttribute
{
//...
}
Obtaining an Objects Attributes Once an attribute has been attached to an item, other parts of the program can retrieve the attribute. To retrieve an attribute, you will usually use one of the two methods. The first is GetCustomAttributes(). It retrieves a list of all attributes attached to an item and has the general form:
object[] GetCustomAttributes( bool searchBases )
If searchBases is true, then the attributes of all the base classes through the inheritance chain will be included. Otherwise, only those attributes defined by the specified type will be found.
The second method is GetCustomAttribute().One of its forms is shown here:
static Attribute GetCustomAttribute( MemberInfo mi,Type attribtype )
Here, mi is a MemberInfo object that describes the item for which an attribute is being obtained. The attribute desired is specified by attribtype. You will use this method when you know the name of the attribute you want to obtain. For example ,to obtain reference to the OneAttribute, you can use this sequence:
//Retrieve the RemarkAttribute.
Type remarkAttribute = typeof( OneAttribute );
OneAttribute ra=( OneAttribute ) Attribute.GetCustomAttribute( t , remarkAttribute );
Once you have a reference to an attribute you can access its members. Thus information associated with an attribute is available to a program that uses an element to which an attribute is attached. For example the following statement displays the remark field.
Console.WriteLine( ra.remarks );
Here’s the whole program.
//Attribute Example.
using System;
using System.Reflection;
[AttributeUsage( AttributeTargets.All )]
public class OneAttribute : Attribute
{
string data;
public OneAttribute( string comment )
{
data = comment ;
}
public string remarks
{
get
{
return data;
}
}
}
[OneAttribute( "Here's a class that uses an attribute." )]
class GetAttribute
{
//...
}
class DemonstrateAttribute
{
public static void Main()
{
Type t = typeof( GetAttribute );
Console.Write( "Attributes in " + t.Name + ": " );
object[] attribs = t.GetCustomAttributes(false);
foreach( object o in attribs )
{
Console.WriteLine(o);
}
Console.Write( "\n\nRemark: " );
//Retrieve the RemarkAttribute.
Type remarkAttribute = typeof( OneAttribute );
OneAttribute ra=( OneAttribute ) Attribute.GetCustomAttribute( t , remarkAttribute );
Console.WriteLine( ra.remarks );
Console.Read();
}
}
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|