0

Possible Duplicate:
Real world use of custom .NET attributes

Just like the title said I'm learning reflections in .NET and I've read that custom attributes gives you a lot of power because you can read them as metadata in run-time and make decisions or you can use them for documentation purposes, but I really don't see the power of them, what is the big deal ?

In concrete, could you give some examples of where custom attributes are useful?

Thanks!

Community
  • 1
  • 1
franko_camron
  • 1,288
  • 4
  • 14
  • 30
  • See how MVC uses custom attributes to provide custom validation: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx – StriplingWarrior Dec 07 '11 at 23:38

1 Answers1

2

We use CustomAttributes for several different purposes:

  1. To record database field names for Properties whose names are different than those of the database table they are stored in. (We use reflection to automatically persist classes into the database).
  2. To indicate whether or not an empty property (0 numeric, empty string) should be stored in the database as null.
  3. To store nomenclature mapping (nomenclature is stored in the database and different properties in a class may represent the same data and we don't want to have nomenclature entries for each of these properties).
  4. To indicate which properties in a class can be automatically included in a mail merge application.

etc.

Julien Roncaglia
  • 17,397
  • 4
  • 57
  • 75
competent_tech
  • 44,465
  • 11
  • 90
  • 113