I asked too much by .NET developers about attributes and their meaning. I hope that this post will help you to understand it better.
Introduction: what are attributes?
An attribute is a powerful .NET language feature that is attached to a target programming element (e.g., a class, method, assembly, interface, etc.) to customize behaviors or extract organizational information of the target at design, compile, or runtime. It is a clean approach to associate metadata with program elements and later use the metadata at design, compile or run time to accomplish some common objectives. Attributes come in two flavors: intrinsic and custom. Intrinsic attributes are supplied as part of the Common Language Runtime (CLR), and they are integrated into .NET. Custom attributes are attributes you create for your own purposes. As a thumb-rule, a class is said to be an attribute class, if it directly or indirectly derives from the System.Attribute class.
Why we need attributes?
For example, ObsoleteAttribute, causes a compile-time warning to appear, letting the developer know that a method should no longer be used. ; Attributes are also used extensively in securing .NET assemblies, forcing calling code to be evaluated against pre-defined security constraints. ; DllImportAttribute that allows a program to communicate with the Win32 libraries; and more...
Once associated with a program entity, the attribute can be queried at run time and used in any number of ways.
Attributes are placed before the item that they will be "decorating" and they are put inside square braces .
To understand the power of attributes, consider the serialization of an object. In .NET, you just need to mark a class Serializable to make its member variables as Serializable. Serializable attribute: (type of SerializableAttribute)
public class CustomerData
I didnt use the SerializableAttribute class when applying the attribute to the class. The only time that you can leave off the Attribute portion of the name is when you are applying the Attribute to a class.
public class MyClass
I used the attribute Obsolete, which marks a method (in this case) that should not be used. The first parameter is the string, which explains why the item is obsolete and what to use instead. In fact, you can write any other text here. The second parameter tells the compiler to treat the use of the item as an error. The default value is false, which means the compiler generates a warning for this.
When we try to compile this program, we will get an error with the message: MyClass.Calc is obsolete: 'This method replaced by CalcEx method.'
When do you define your custom attributes? query at runtime! For example, let's create CryAttribute class:
public class CryAttribute : Attribute
public CryAttribute( string reason)
Now, Let's use this attribute:
[Cry("Because babies cry..:)")] // normal babies cry without reason
It's great...but what can we do with it?? .NET gives us a new point of the view: runtime attributes querying.
How can we query?
public string WhyTheBabyCry(Baby baby)
object obj = type.GetCustomAttributes();// the first attribute
Now we can use it:
Baby Baby1 = new Baby();
More about attributes you can find at msdn.