Programming with Attributes
page 3 of 4
by Kavitha Pradeep
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 22422/ 26

Authoring Your Own Attributes

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.


View Entire Article

User Comments

Title: very very nice article   
Name: kasi
Date: 2012-03-17 5:48:04 AM
Comment:
thanks you very very very much Kavitha Pradeep..a nice article

i hope please write one more on attribute based programming

i am waiting
Title: good Example for Attributes   
Name: Sivakumar
Date: 2007-09-05 2:49:09 AM
Comment:
good Example for Attributes
Title: so cool   
Name: prince
Date: 2006-02-13 7:45:48 AM
Comment:
thanks a lot, this website is very useful for me
Title: Good Site   
Name: Anju
Date: 2006-02-10 7:47:42 AM
Comment:
I found it very useful.
Title: comments   
Name: sanjeev
Date: 2005-12-29 5:25:10 AM
Comment:
very good site
Title: great   
Name: devil
Date: 2005-01-18 4:36:48 AM
Comment:
good artical ...as i was searching for this [STAThread]...anyway nice work...want ur help if u r interested as i am a novice programmer into the world of .net...






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-06-15 2:48:41 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search