I saw some of the examples of utilize attribute, e.g.
(as a map for dynamic factory)
http://msdn.microsoft.com/en-us/magazine/cc164170.aspx
Just wondering what is the advantage of using attribute?
I can find the reference on http://msdn.microsoft.com/en-gb/z0w1kczw(VS.80).aspx
however, I am not sure when and why should I try to use it.
In the .NET Framework, attributes can be used for many reasons — like
Defining which classes are
serializable
Choosing which methods are exposed in
a Web service
Attributesallow us to adddescriptionsto classes, properties, and methods at design time that can then be examined at runtime via reflection.Consider this example:
Say you have a class which has a method from older version which is still in use for any reason and now you have come up with a new version of the class which makes fantastic use of Generic List and LINQ and has a new method for similar purpose. You would like developers to prefer the new one provided in the later version of your library. How will you do that ? One way is to write in the documentation. A better way is to use attribute as follow.
If an
obsoletemethod is used when the program is compiled, the developer gets this info and decides accordingly.We may also create and add
Custom Attributesas per requirements.Reference :
Hope this helps