Attributes in C#

Lets see the mean of attributes in c# world.

/* I got a book named Practical .NET2 and C#2 and i think this book has really nice contents related to .NET with C# 2.o, I am taking one of good content related to attribute and discussing with you all. */

What is an attributes ?

An attribute is a information which marks the elements of code such as a class or method. For example .NET framework provides the system.obsoleteAttribute attribute which can be used to mark a method as follows[we use [] brackets to attach attributes to code elements]

void Fun{}

The Fun method is marked with the system.ObsoleteAttribute attribute information is inserted in the assembly during compilation. This information then can be used by the c# compiler. When it encounters a call to method, it can then emit a warning indicating it is better to avoid call to an obsolete method, which risks of going away in future versions. Without an attribute, you would be forced to properly document the fact that Fun() method is now obsolete the weakness of this approach is that you will have no guarantee that your clients will read the documentation and be aware of the fact that method is now obsolete.

When do we need attributes ?

The advantage of using attributes resides in the fact that the information that it contains is inserted into the assembly. This information can then be consumed at various times for all sorts of purposes:

  1. An attribute can be consumed by the compiler. The System.ObsoleteAttribute attribute that we have just described is a good example of how an attribute is used by the compiler, certain standard attributes which are only destined for the compiler are not stored in the assembly. For example, the SerializationAttribute attribute does not directly mark a type but rather tells the compiler that type can be serialized. Consequently, the compiler sets certain flags on the concerned type which will be consumed by the CLR during execution such attributes are also named pseudo-attributes.
  2. An attribute can be consumed by the CLR during execution. For example the .NET Framework offers the System.ThreadStaticAttribute attribute. When a static field is marked with this attribute the CLR makes sure that during the execution, there is only one version of this field per thread.
  3. An attribute can be consumed by a debugger during execution. Hence, the System.Diagnostics.DebuggerDisplayAttribute attribute allows personalizing the display of an element of the code(the state of an object for example) during debugging.
  4. An attribute can be consumed by a tool, for example, the .NET framework offers the System.Runtime.InteropServices.ComVisibleAttribute attribute. When a class is marked with this attribute, the tlbexp.exe tool generates a file which will allow this class to be consumed as if it was a COM object.
  5. An attribute can be consumed by your own code during execution by using the reflection mechanism to access the information. For example, it can be interesting to use such attributes to validate the value of fields in your classes. Such a field must be within a certain range. Another reference field must not be null. A string field can be atmost 100 character. Because of the reflection mechanism, it is easy to write code to  validate the state of any marked fields. A little later, we will show you such an example where you can consume attributes by your own code.
  6. An attribute can be consumed by a user which analyses an assembly with a tool such as ildasm.exe or Reflector. Hence you could imagine an attribute which would associate a character string to an element of your code. This string being contained in the assembly, it is then possible to consult these comments without needing to access source code.

Things to know about attributes

  1. An attribute must be defined by a class which derives from System.Attribute.
  2. An Instance of the attribute class is only instantiated when the reflection mechanism accesses one of its representatives, Depending on its use, an attribute class in not necessarily instantiated(as with the System.ObsoleteAttribute class which does not need to be used by the reflection mechanism).
  3. The .NET framework puts several attributes to your disposition. Certain attributes are destined to be used by the CLR. Other are consumed by the compiler or tools supplied by Microsoft.
  4. You have the possibility of creating your own attribute classes, They will then necessarily be consumed by your program as you cannot tinker the compiler or the CLR.
  5. By convention, the name of an attribute class is suffixed by Attribute. However, an attribute named XXXAttribute can be used in c# both using the XXXAttribute expression but also with the XXX expression when it marks an element of the code.

I will come with more stuff related to Custom Attributes in next article. 


Practical .NET2 and C#2 By Patrick Smacchia