一切福田,不離方寸,從心而覓,感無不通。

C# Attributes in 5 minutes

What are attributes and why do we need it?

How can we create custom Attributes?

Is it possible to restrict a custom attribute to a method only?

Other than information what more can we do?

Do attributes get inherited ?

What if we want some attributes to be prevented from inheriting?

If I want an attribute to be used only once in a program?

What are attributes and why do we need it?

“Attribute is nothing but a piece of information”.

This information can be attached to your method, class, namespace, assembly etc. Attributes are part of your code this makes developers life easier as he can see the information right upfront in the code while he is calling the method or accessing the class and take actions accordingly.

For instance below is a simple class where “Method1” is decorated by the “Obsolete” attribute. Attributes are defined by using the “[]“ symbol. So when developers starting coding in this class they are alerted that“Method1” is obsolete and code should be now written in “NewMethod1”.

In the same way if somebody is trying to create objectof “Class1” he gets an alert in the tool tip as shown in the below code snippet that “Method1” is obsolete and he should use “NewMethod1”.

So in short Attributes are nothing small piece of information which is embedded declaratively in the code itself which developers can see upfront.

In case you want to show some message to the developers you can pass the message in the “Obsolete” attribute as shown in the below code snippet.

If you want to be bit strict and do not want developers to use that method, you can pass ‘true” to the “Obsolete” attribute as shown in the below code.

Now in case developer tries to make a call to “Method1” they will get error and not just a simple warning.

How can we create custom Attributes?

The “Obsolete” attribute which we discussed at the top is a readymade attribute To create a custom attributes you need to inherit from the attribute class. Below is a simple “HelpAttribute” which has a “HelpText” property.

“HelpAttribute” is applied to the “Customer” as shown in the code below. Now developers who see this class , see the information right in the front of their eyes.

Is it possible to restrict a custom attribute to a method only?

By using the “AttributeUsage” and “AttributeTargets” you can restrict the attribute to a particular section like class , method , property etc. Below is a simple custom attribute is now confined only to methods.

If you try to apply the above attribute over a class or property you would get a compile time error as shown below.

You can also restrict attribute’s to a class , constructor , assembly etc. Below is the list of possibilities to which you can confine your attribute.

Other than information what more can we do?

One use which we have discussed till now is for the developer’s that they can see the information while coding and take decision accordingly. Other use is you can read the information programmatically using reflection and act on it.

For instance below is a custom attribute which describes length of characters for a property of a class.

Below is a customer class over which that property is decorated providing information that the maximum length of “CustomerCode” property cannot exceed 10 character.

Below goes the code which will loop dynamically through the attributes of each property and do the length validation check.

So the first step is to create the object of the customer class.

Second step is to get the “Type” of the object. Because once we get the type of the object we can browse properties, methods etc of the object.

Use the “Type” object and loop through all properties and attributes of those properties.

Once you get hold of the attribute you can do the length check and raise exception accordingly.

Do attributes get inherited ?

Yes, they get inherited in the child classes.

What if we want some attributes to be prevented from inheriting?

We have an “Inherited” property in “AttributeUsage” if we set it to false those attributes will not be inherited in the child classes.

If I want an attribute to be used only once in a program?

If you specify “AllowMultiple” as true it can be used multiple times in the same program.

See the below facebook video which explains and demonstrates C# Attributes practically.

from:http://www.codeproject.com/Articles/827091/Csharp-Attributes-in-minutes#HowcanwecreatecustomAttributes