During the implementation of my polymorphic enum, I have chosen to allow several types for the underlying “ordinal” value, as it is also possible for standard C# enums…
From the MSDN documentation, the allowed underlying types for enum types are the following (although other value types such as char are eligible, their use is not recommended) :
|sbyte||Signed 8-bit integer|
|byte||Unsigned 8-bit integer|
|short||Signed 16-bit integer|
|ushort||Unsigned 16-bit integer|
|int||Signed 32-bit integer|
|uint||Unsigned 32-bit integer|
|long||Signed 64-bit integer|
|ulong||Unsigned 64-bit integer|
In order to enable the ordinal parameter to be optional in the registration of enum values, I had to find a way to compute an automatic value. The rule I wanted to apply was very simple : if there is no value already register, use zero, else use the maximum value already registered, plus one. But how do you write such code without checking the type in an ugly fashion ?
Generic constraints are somehow limited : I can force the ordinal type to be a value type by using a “where T : struct” constraints, but there is no such thing as a “IPrimitiveNumericValue” interface !
Here is where I got to, using generics and lambdas…
I first define static class and a private static helper function that take a Func<T, T> and returns… the unmodified argument. This is just a hint for the compiler, because I’m going to use lambdas and as you might know, a lambda can either be converted to a delegate (code) or to an expression tree representing the lambda (data). Without any indication, the compiler can’t infer the expected type, so we have to help him…
Then, the class defines a static field and initialises it in its static constructor :
At this point, all the required incrementing functions are expressed as lambdas and registered in a dictionary.
The only method left to write is the PlusOne method :
Type checking and dictionary lookup. That’s it !