views:

529

answers:

7

What kind of things have you used custom .NET attributes for in the real world?

I've read several articles about them, but I have never used custom attributes.

I feel like I might be overlooking them when they could be useful.


EDIT

I am talking about attributes that you create, not ones that are already included in the framework.

A: 

I had to serialize some objects to a custom (legacy) format, and I used attributes to identify which fields should be serialized and how to format them. Then I had a serializer that could take any object with these attributes and use reflection to format it.

Robert
You weren't really using your own custom attributes, were you?
Esteban Araya
heeeeeeeeeeeeee
abmv
A: 
rajesh pillai
Did you write the attributes?
Esteban Araya
The Table, Column, BenchMark etc are objects derived from Attribute class. Besides the parser engines takes care of handling it. These objects are always instantiated by some factory class, so that you can get a hook on to the methods.
rajesh pillai
And please don't confuse with the similar attribute for LINQ which you have now. This is an old bit of code with where the attributes were lying in it's own namespace.
rajesh pillai
+3  A: 

I've used them "custom" attributes for validation (ie. marking a field to be validated with my own "credit card validation") and custom LinqToLucene analyzers I've written (ie. specifying which analyzer to use on a given field).

The validation code, for example, would look something like this:

public class Customer
{
     [CreditCardValidator]
     string creditCardNumber;

     [AddressValidator]
     string addressLineOne
}

When the object above is validated, each field is validated with the appropriate validator thanks to the "custom" attribute.

In the LinqToLucene stuff I've written custom attributes are nice because they allow you to find (through reflection) specific fields at run time. For example, if you have a customer object, you may be interested in getting all the properties that have been marked as "index me": a custom attribute lets you do this easily since it exposes meta-data about the object in a manner that is easy to query.

Esteban Araya
+1  A: 

Among other things, I've used them to specify EBNF which is read at run-time to create custom parsers on the fly and also to specify metadata about fields for a database.

I find one 'pattern' I'm commonly using custom attributes is to replace enums especially when there is a dependency on the enum in diff places in code.

E.g. I might have an enum for a state of an object. Based on this state, I have maybe 3 or 4 different places in code which I would do a 'switch' of that enum and perform some operation. Some other developer could easily introduce a bug by adding a new enum but not handling in one of the switch statements somewhere else in code.

So to avoid this I create a custom attributes declared to a static class. The custom attributes are loaded in the static constructor of the class into a dictionary and all places in code use the dictionary instead of switch statements. The custom attribute constructor contains the 'hard-coded' values for each switch statement.

fung
+4  A: 

I created a scripting engine, and tagged various methods with the [Command] attribute. This meant that these functions were exposed to the scripting engine.

Example:

[Command(HelpText = "Lists active users")]
void ListUsers(void)
{

}

[Command(HelpText = "Terminate a specific user's connection")]
void EndConnection(int userID)
{

}

And as used:

MyScriptEngine>>  Help
Available Commands are:
    ListUsers: Lists active users
    EndConnection {userID}: Terminate a specific user's connection

MyScriptEngine>> EndConnection 3
    User 3 (Michael) has had his connection terminated.

MyScriptEngine>>
abelenky
A: 

I haven't really found a use for custom attributes as of yet. There have been a few situations where I thaught they may be appropriate but didn't use them because apparently the reflection involved in reading custom attributes is quite expensive.

Crippledsmurf
A: 

I have placed custom attributes on classes within "plug-in" DLLs. This allows a framework to dynamically discover available plug-ins, evaluate whether they are of interest, and then dynamically load the ones of interest.

In our domain, the example is plug-ins which model particular vehicles within a family. One plug-in for a vehicle family might actually model several vehicle models within the vehicle family (e.g., "MX-6", "Probe"). If an ID or Model Name is included as a custom attribute array, we can quickly ignore any DLLs that don't even have custom attributes, and then further ignore any that do not model the vehicle of interest.

e-holder