§ April 4, 2007

In .NET enums are a strongly typed flag-like object. Flags would normally defined as a macro, or constant variable. The trouble with typical numeric flags is that a) the coder using the flag may or may not know all of the possible values, or b) might accidentally type the wrong constant or macro. Since the flag is just some magic number they may end up with a logic error that may be hard to find under certain circumstances. With enums I can provide the same logic (some numeric flag) but create a type that I can use to enforce some subset of numbers that are checked at compile time, and can be verified at run time ( Enum.IsDefined ). As with everything, enums have their faults. What if I want to extend some enum object (polymorphic enums?), or give it additional functionality or meta data. Yesterday at work we ran across a case where being able to extend an enum was mandatory. All of our enums have numeric and string values, and are typically written like:

the FriendlyName attribute enables you to have both a numeric flag value as well as some human readable value of the enum element other than "One" or "Two" or "SomeEnumValue"(ie: Foo.One.ToString())

Our company uses code generation like most fish use water. We use enums quite a bit and each enum type and all of their values are stored in a database which is used by the code generation tool to generate 99% of our business and data layer code. Since our code is generated, if we go in and manually add additional values that don't need to be stored in the database, our generation tool will blow away any hand written changes on every build. We would have killed to have something like a partial enum, but alas, no such thing exists.

I set out to try to create a "Fake Enum" class that acted and felt like an enum but allowed us to extend it by way of the partial class or inheritance. Well after a little poking around, inheritance flew out the window (just over complex without resorting to some generics base type), but the ability to do a partial fake enum was definitely doable.

Since additional partial classes would be added, I didn't want to have to provide some functionality of ToString or explicit conversion for each partial class, so I resolved the ability to get the numeric value, string member name value through reflection. Since I had full control over the type, I decided it would be nice to add the FriendlyNameAttribute functionality into my fake enum, so I did.

Its implementation uses .net reflection to iterate over its members and obtain the enum like member and its associated and values (since every member we use like an enum will be a static instance of itself) and additionally its friendly name. actually we could make our fake enum non-numeric based (this would kind of defeat the ability to do a flag like operation) but that wasn't something needed at the time so we just stuck with int.

To be able to use these objects like a bit field (like enums are), one would need to add the | and & operators so that fields could be combined or extracted (perhaps if I'm bored I'll add this to a future version).