When should an attribute be used in C #?

I saw some examples of using an attribute, for example. (as a map for a dynamic factory) http://msdn.microsoft.com/en-us/magazine/cc164170.aspx

Just wondering what is the advantage of using an attribute? I can find a link to http://msdn.microsoft.com/en-gb/z0w1kczw(VS.80).aspx however, I'm not sure when and why I should use it.

+42
c # attributes
Feb 19 '10 at 9:17
source share
5 answers

In the .NET Framework, attributes can be used for many reasons - for example,

  • Defining serializable classes

  • Choosing Methods That Web Services

Attributes allow you to add descriptions to classes, properties, and methods at design time, which can then be viewed at run time through reflection.

Consider this example:

Say you have a class that has a method from the old version that is still in use for some reason, and now you have created a new version of the class that uses the fantastic use of Generic List and LINQ and has a new method for a similar purpose. You would like developers to prefer the new one provided in a later version of your library. How do you do this? One way is to write to the documentation. It is best to use the attribute as follows.

 public class AccountsManager { [Obsolete("prefer GetAccountsList", true)] static Account[] GetAccounts( ) { } static List<Account> GetAccountsList( ) { } } 

If the obsolete method is used when compiling the program, the developer receives this information and decides accordingly.

AccountManager.GetAccounts () is deprecated: prefer GetAccountsList

We can also create and add Custom Attributes as required.

Link:




Hope this helps

+28
Feb 19 '10 at 9:23
source share

My recommendation: use attributes to indicate facts about the mechanisms, but not to model aspects of your business area.

More details:

http://blogs.msdn.com/ericlippert/archive/2009/02/02/properties-vs-attributes.aspx

+19
Feb 19 '10 at 16:30
source share

Attributes are suitable if you want to attach metadata to your classes or class members, as well as when applying common behavior without having to implement a specific interface for each unit that shares the behavior. The latter is an example of aspect-oriented programming .

+5
Feb 19 '10 at 9:26
source share

Consider the attribute as metadata about the method or property to which it belongs. He is telling something about a member.

+4
Feb 19 '10 at 9:26
source share

The .NET Framework predefines and uses attribute types to control application runtime.

Consider the [webmethod] attribute; in the runtime, it resolves this attribute and determines that this method will be displayed in the web service.

In the same way, you can write your custom attributes to control the behavior of your application at runtime. Attributes can target classes, methods, properties, delegate, enumeration, event, field ...

To enable the attribute at runtime, you must use reflection.

See MSDN for more information.

0
Feb 19 '10 at 9:25 am
source share



All Articles