Posts
20
Comments
10
Trackbacks
0
Attributing: Fancily dressed Classes
Creating Attribute Classes in C# .Net

Recently I have found the need to add attributes to a class. The plan was to reflect a child form’s attribute’s properties from the base from and in this way gain access to the forms display name and the name of the data type that the form would be displaying. By doing this we would not need to force each child form to implement a pointless interface to provide this detail. It turned out that attributes were easy to make, easier to use and provided a wealth of functionality.

This "mini" post will hopefully introduce you to attributes and provide you with a slightly better understanding of what they actually do.

[Section (“Start”)]
So I guess we need to start by introducing attributes and explaining what they are. It turns out that attributes are classes. Yes, those funny little [] things on top of your classes and methods are for lack of a better explanation, instances of attribute classes. By declaring [Naming("Window Location Capture")] we are creating an instance of the Naming attribute class and passing the string “Window Location Capture” to its single parameter constructor.

[Section (“Well know Attributes”)]
To realise that these attribute classes affect our every day programming lives think of the UI code that you use when creating a custom control. In order to set up the controls behavior in the properties window we decorate our control’s properties with [Browsable(true)] and/or [Category("Foo Category")]  and/or [Description("Foo")]. Doing so allows us to specify that the property must be displayed in the property box. That it must fall under the category called Foo Category and that it must display “Foo” as the description for this category.

The above were all property attributes, that being they can only be applied to properties. A well knows class attribute is the [Serializable] attribute. You will notice that this attribute takes no parameters.

[Section (“Enough with the basics”)]
Right, so on with how to make your own attribute.

Doing this is rather easy and involve three basic steps.
1.   

     [AttributeUsage(AttributeTargets.Class)]

Deciding what type of attribute you want to create: Assembly, Class, Constructor, Delegate, Enum, Event, Field, GenericParameter, Interface, Method, Module, Parameter, Property, ReturnValue or Struct.
I will not even try and give you an example of each, but rather let you choose and research which you want to use.

2.   

    public class Naming : Attribute

Inherit the Attribute class.

3.

    public string DisplayName { get; set; }

Add you properties and set up the constructor.

That’s it. You now have an attribute class that can be applied to whatever you decided you wanted to apply it to.

[Section  (“Reading your attributes”)]
So everyone is now thinking great! We have attributes on all our methods, now what do we do. Well to make use of these attributes we need to use reflection. By reflecting the class or property that we are looking for we can query if attributes have been applied. If attributes have been applied we can retrieve them and gain access to their properties.

We get the type that we are working with. In this case we are working form a base form and accessing the attributes on the child class.

   Type t = this.GetType();



Having retrieve the type of the child class we check if the specific attribute has been applied to the class.               

   if (Attribute.IsDefined(t, typeof(Naming)))



If the attribute has been applied we can gain access to an instance of that attribute.

   Naming myAttrib = (Naming)Attribute.GetCustomAttribute(t, typeof(Naming));



Now we can gain access to the properties of this attribute.

   this.Text = "Attribute Example | " + NamingAttrib.DisplayName;



[Section  (“Further examples”)]

I have mocked together some code that displays some the principles that I have talked about. Hopefully you will be able to find enough information to work out how you can use attributes.

There is also a write up on attributes over at MSDN http://msdn2.microsoft.com/en-us/library/aa288059(VS.71).aspx
posted on Tuesday, April 15, 2008 5:18 PM Print
News