Here are the bits, and please excuse my cheesy example, or just make fun of it.
I use an interface which my custom attributes derive from, in addition to deriving from System.Attribute. The interface merely enforces a property named Value to be defined. The interface is used in a later static method, as you will see.
I create a custom attribute deriving from System.Attribute and my interface.
I add my custom attribute to a targeted enum.
I use a static generic method to retrieve the custom attribute value for a given enum value.
I retrieve my attribute value in code with a simple "one-liner".
When I see another need for a custom attribute and I already have a solid pattern in place. Just add a new attribute class, modify my enum, and off I go.
In my applications, I continue to find more and more ways to use attributes and enums together. Hope you find this useful as well.
This comment has been removed by the author.
ReplyDeletewhere do you put the static method
ReplyDelete>where do you put the static method
ReplyDeleteI think in:
public static class EnumHelper
Thanbks Joel!! You have honestly saved me from hundreds of lines of patched code!!!!
ReplyDeleteIts really great piece of code to understand attributes.
ReplyDeleteThank you Joel
Great piece of code! thanks :)
ReplyDeleteVery nice, thanks
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteGood work - now we could create NoSQL database based on Enum and Custom attributes (just kidding :)).
ReplyDelete