Introduction

This article is about a very light-weight enum extension library that takes
advantage of dynamic types in .NET 4.0 to provide a simple way to add meta
attributes to an enum field. While the code and demo are primarily intended for
WPF usage, you should be able to trivially use the same classes in Silverlight (reasonably
sure it'll work) or Windows Forms (not as sure of this one), with the
restriction that you have to use .NET 4.0 or higher.

Design Goals

It should be effortless to use the extensions with large existing code
bases that have several enum types.

WPF data-binding should work on these custom attributes, with
customizable bindable properties.

Existing properties/fields should require minimal or no code changes.

Trouble-free access to the underlying enum (meaning I didn't want to
hide the enum with a dominant wrapper class).

Thin interfaces to facilitate custom extension attributes.

Minimal wrapper overhead - there should not be be multiple wrapper
instances for the same enum type.

I hope I have achieved all these design goals in my implementation. I'll
quickly talk about how this library can be used and then jump into the
implementation details.

Code usage

Consider the following enum class to which I have added some custom
attributes.

EnumDisplayName is an attribute that allows you to specify a
custom display name to an enum field (and I know there are dozens of such
implementations already available). Since it's such a common requirement, this
extension comes with the library (only a few lines of code anyway).
StandardExtendedEnum is also an included extension class that lets you
add a custom data-bindable property, and in this example I've added a property
called InStock of type bool.
InterestingItemImage is a custom extension class specific to the demo
project which allows you to associate an image with an enum field. Here's how
the code for InterestingItemImage has been implemented and I'll
discuss the details in the next section although I think the code is self
explanatory (which is always an important design goal).

In this example I've implemented the INamedExtendedEnumAttribute
interface although if I did not want to provide a custom property name, I could
have just implemented the IExtendedEnumAttribute interface (just a
single GetValue method). Now here's how I set up the bindable
properties in my datacontext class (which in my simple example also happens to
be the view class).

I get the enumeration values using Enum.GetValues and then
convert that into an ExtendedEnum<> collection using LINQ
Select and an explicit cast. Now here's a SelectedItem
property that will fetch the selected enum field back from the collection that's
bound to some WPF control (in the demo, a listbox).

One very important thing there is that the backing field is of the original
enum type. There are implicit conversions in place (two way) so you can convert
between an enum and the extension class transparently. And now take a look at
the XAML.

Example 1

This is one of the simplest examples and the only extra functionality you get
here is that this respects the display name attributes (when they are provided).
This may probably be one of the most common uses of this library.

Example 2

Woah! What happened there? In the previous example, the display name was
fetched when one was provided and where one was not, it used the default
enum
name. That's because the previous example relies on the default ToString
implementation. But in the above example, I have explicitly specified the
DisplayMemberPath property. This means I am telling WPF to
specifically look for that property (in our case it'll be an attribute), but in
the demo enumeration, two fields do not have display name attributes and so
they'll show up blank. This is just one of those gotchas that you need to
be careful about. It's easily circumvented by taking advantage of the
ToString implementation, so it's unlikely to be a show stopper.

As you can see, the dynamic property can itself be an enum
which itself has dynamic properties. So yeah, there's no end to the madness
here!

Implementation details

At the core of the class are the two interfaces that the extension attributes
need to implement, and the most basic (and mandatory) interface is
IExtendedEnumAttribute.

publicinterface IExtendedEnumAttribute
{
object GetValue();
}

This interfaces lets you provide the value associated with a custom attribute
property. The dynamic property name will default to the name of the attribute
(minus the Attribute suffix). If you want to customize the name too, then
use the INamedExtendedEnumAttribute interface.

And then there's the flexible and yet mostly simple
StandardExtendedEnumAttribute which lets you quickly add attribute
property values without having to implement a full class for it (although in
scenarios where you need custom processing, as in the image class I showed
above, you will have to write a custom attribute class).

The class derives from DynamicObject, extracts the attributes
via reflection, and exposes them to dynamic-aware call-sites by overriding the
TryGetMember, TrySetMember, and
GetDynamicMemberNames methods. It also caches the extended wrappers so
that you will always have at maximum one wrapper instance per original
enum
type. Notice how there's no need to lock write-access to the dictionary since
it'll only be called from the constructor. So this class is thread safe (for
most common purposes).

I will be delighted if you could give me some feedback and criticism, and by
the way feel free to shower extraordinary compliments on me! *grin*

Share

About the Author

Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.

Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com.

The cricket world cup is going on, after that IPL matches & also never ending matches after that, so until that's done you may notice that I am absent from the Q-A forums. So my rep score has not moved in the last 2-3 weeks & also soon