Advertisment

Annotate your .NET application

author-image
PCQ Bureau
New Update

In C++, the constants, attributes and operations are annotated with public, private or protected keywords. The C++ compiler interprets this annotation to add special meaning for each annotation, thereby giving access and security to data.

Advertisment

Attribute-based programming helps to annotate classes, constructors, properties and methods to add more meaning to them during runtime. The attributes can be added or removed from the code conveniently and the duplication of code across methods is eliminated. The runtime uses these attributes to compose an object stack when the object is created. When the client makes a method call, each attribute gets a chance to execute its part and transfer control until finally the actual method is invoked. During the stack wind up, each attribute is again given a chance to wind up and the final stack wind up takes place.

This means that the sequence of operations that you wrote is now handled by the runtime. Intelligent usage of attributes gives flexibility to code and avoids duplication of code. 

Let’s look into the recipe of .NET to design attributes, write custom attributes and use pre-defined attributes. We will take up a simple example wherein we use attributes to annotate each class in a .NET application with the developer of the class. 

Advertisment

Attribute-based programming in .NET



The .NET Framework makes life easy by extensively supporting attributes. You could add flavor to your code by using the existing attributes or you could define and design your own attributes. The base class for attributes is

System.Attribute. 

Using existing attributes is relatively easier and Microsoft has provides a huge list of attributes that you could use. You must have definitely developed simple console applications. 





static void Main(string<> args)


{


}

Advertisment

The annotated before the main method is an attribute that marks a thread to use the Single-Threaded COM Apartment if COM is needed. Similarly, if you have developed Web services, then you must have seen the attribute annotated before methods act as Web services. Thus using these attributes is pretty easy.

We shall now delve into how to write our own custom attributes. Writing custom attributes greatly helps in annotating pieces of software code according to our needs. The basis for Attributes is the System.Attribute class.

Authoring your own attributes 



When we play around with attributes we need to wear three hats at any point of time




l
The attribute creator




l
The attribute user




l
The attribute interpreter

Advertisment

Let us briefly understand the steps involved in writing attributes. The points to remember are  

Your class must be derived from System.Attribute

Every class needs to have a default constructor (at the minimal)

Advertisment

If your attribute takes parameters then it must have a constructor that takes parameter

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

Advertisment

using System;



namespace DeveloperTrackTool


{


///





/// Summary description for Sales.


///







/// 


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.

Advertisment





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 that is annotated with the attribute that we have created, ie, 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, if you 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. 

Kavitha Pradeep,

Wipro

Advertisment