When we play around with attributes we need to wear three hats at any point of time
a) The attribute creator
b) The attribute user
c) The attribute interpreter
Let us briefly understand the steps involved in writing attributes. The points to remember are
a) Your class must be derived from System.Attribute
b) Every class needs to have a default constructor ( at the minimal)
c) If your attribute takes parameters than it must have a constructor that takes parameter.
Step 1 Writing attributes
We shall write a simple ClassDeveloper attribute. This attribute when used for a class would tell us who the owner of the class is or who has coded this class.
The code for this attribute is as given below
using System;
namespace DeveloperTrackTool
{
/// <summary>
/// Summary description for Sales.
/// </summary>
///
class ClassDeveloperAttribute:Attribute
{
public string loggerName;
public ClassDeveloperAttribute(string strName)
{
loggerName = strName;
}
}
}
To help you understand attributes we shall write a simple code .In the above code we have written a class called ClassDeveloperAttribute that is inherited from Attribute. This class has a single data member and a constructor. We have thus created a custom attribute called ClassDeveloper . This attribute can now be applied to other classes whenever we need to track who the developer of the class is. Thus we have worn the hat of a creator.
[ClassDeveloper("Kavitha")]
public class Sales
{
public Sales()
{
//
// TODO: Add constructor logic here
//
}
}
}
Now let us start using this attribute. The Sales class is a very simple class which is annotated with the attribute that we have created i.e the ClassDeveloper attribute. It is being passed the developers name who has created the Sales class . So now we have worn the hat of a user.
Let us now write a simple code to interpret this attribute. Create a simple form based application with a textbox and a button .
When the user clicks on the button we would instantiate a Sales object and invoke a method called DisplayDeveloper() that would display all the attributes applied to the Sales class
private void button1_Click_1(object sender, System.EventArgs e)
{
Sales s1 = new Sales();
DisplayDeveloper (s1);
}
Let us take a look at the DisplayDeveloper() method in detail
public void DisplayDeveloper(object obj1)
{
Type typ = obj1.GetType();
object []obj = typ.GetCustomAttributes(true);
if ( obj[0] is ClassDeveloperAttribute )
textBox1.Text = " Class Developed by " + ((ClassDeveloperAttribute)obj[0]).loggerName.ToString();
else
textBox1.Text= "No Owner for this class";
}
The method gets the type of the object passed as the parameter to the method. In this case a Sales object . The GetCustomAttributes() function helps in getting the custom attributes for the Sales class. Currently the attribute for the Sales class is ClassDeveloperAttribute. The attributes are then displayed within a textbox
Now in future if you would need to also check who has debugged the Sales class probably the easiest thing you could do is to create the ClassDebuggerAttribute and then annotate the Sales class with this attribute.