18

Could anyone explain the benefits (or reasons) to use custom attributes in your code. Of course I use (and understand the purpose of) defined attributes in certain scenarios (WCF, Serialization etc.), but I cannot imagine any algorithms where I would need to create and use my own custom attributes. Could someone provide a real-world case where usages of custom defined attributes bring something to a project.

Slava Kovalchuk
  • 523
  • 4
  • 7

8 Answers8

9

The same reason as for WCF etc, but something that's specific to your project - you want to add some metadata to some members (types, fields, methods, whatever) to specify something about the mechanism involved, and it's not something which is covered by existing attributes.

For example, NUnit wanted to add their own indication that a particular type contained unit tests - there was no such existing attribute, so they created TestFixtureAttribute.

It's a relatively rare event, sure - but it can happen.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

If you want to write your own system like WCF, Serialization, etc...

If you write code that iterates over types or members and does things with them, you will frequently want to use your own custom attributes to mark some members as being different or special.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

I regularly use custom .Net attributes to support tooling in my infrastructure. One example was from very early in the .Net days (C# 1.0 to be exact). I was working on a research project which had a native C++ front and a brand new C# back end written by yours truly.

The front and back end shared a very similar object model which was evolving very rapidly. Not wanting to have to hand code both a C++ front end model, C++ serialization mechanism and a C# serialization mechanism I chose instead to attribute my C# types with custom attributes. They told me the parts of the model which were shared between the front and back end.

Once those attributes were in place I wrote a quick and dirty tool which

  • Parsed out the attributes to construct the core shared model
  • Generated the C# serialization code
  • Generated the C++ code
  • Generated the C++ serialization code

This made it dirt simple to keep my model up to date between my 2 projects. Just change the C# code, compile and re-run my tool.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
2

I have used annotations in a custom AOP (Aspect-Oriented Programming) system I developed a while back. Attributes are also very useful for controlling orthogonal concerns like code generation.

Dave Swersky
  • 34,502
  • 9
  • 78
  • 118
1

They can be used for marking tests, as in MBUnit for example. They can also be useful for code that inspects and loads classes (like a Plugin system) to provide meta-information.

Joe
  • 46,419
  • 33
  • 155
  • 245
1

Custom validation is a very good use case and can be seen from these links:

http://odetocode.com/blogs/scott/archive/2011/02/21/custom-data-annotation-validator-part-i-server-code.aspx

How to create Custom Data Annotation Validators

Community
  • 1
  • 1
Gary.S
  • 7,076
  • 1
  • 26
  • 36
1

They are really useful in building object mappers / ORM tools as well. If you ever decide to roll your own mapping system they are almost "required" to get all the functionality one would need. It's used more for making methods / classes more generic and using reflection to determine how to handle objects / select objects /etc...

tsells
  • 2,751
  • 1
  • 18
  • 20
0

To give you a specific case where I've used them. I once had to interact with a Mainframe screenscraper. I created a custom attribute to annotate which fields I wanted to send from my classes to the Mainframe, names that fell outside of conventions, special rules to deal with formatting and collections. I then had a class which was able to reflect over instances and realise which subset of fields were needed to interact with the mainframe screen scraper appropriately.

Martin Clarke
  • 5,636
  • 7
  • 38
  • 58