Anyone Know a Good Workaround For the Lack of an Enum Generic Constraint

Anyone know a good workaround for the lack of an enum generic constraint?

EDIT: This is now live in version 0.0.0.2 of UnconstrainedMelody.

(As requested on my blog post about enum constraints. I've included the basic facts below for the sake of a standalone answer.)

The best solution is to wait for me to include it in UnconstrainedMelody1. This is a library which takes C# code with "fake" constraints such as

where T : struct, IEnumConstraint

and turns it into

where T : struct, System.Enum

via a postbuild step.

It shouldn't be too hard to write IsSet... although catering for both Int64-based and UInt64-based flags could be the tricky part. (I smell some helper methods coming on, basically allowing me to treat any flags enum as if it had a base type of UInt64.)

What would you want the behaviour to be if you called

tester.IsSet(MyFlags.A | MyFlags.C)

? Should it check that all the specified flags are set? That would be my expectation.

I'll try to do this on the way home tonight... I'm hoping to have a quick blitz on useful enum methods to get the library up to a usable standard quickly, then relax a bit.

EDIT: I'm not sure about IsSet as a name, by the way. Options:

  • Includes
  • Contains
  • HasFlag (or HasFlags)
  • IsSet (it's certainly an option)

Thoughts welcome. I'm sure it'll be a while before anything's set in stone anyway...


1 or submit it as a patch, of course...

Is there a workaround for generic type constraint of special class Enum in C# 3.0?

EDIT: A library is now available supporting this via ildasm/ilasm: UnconstrainedMelody.


Members of the C# team have previously said they'd like to be able to support where T : Enum and where T : Delegate, but that it's never been a high enough priority. (I'm not sure what the reasoning is for having the restriction in the first place, admittedly...)

The most practical workaround in C# is:

public static bool HasFlags<T>(this T value, T flags) where T : struct
{
if (!(value is Enum))
{
throw new ArgumentException();
}
// ...
}

That loses compile-time checking for the "enum-ness" but keeps the check that you're using the same type in both places. It has the execution-time penalty of the check as well, of course. You can avoid that execution-time penalty after the first call by using a generic nested type for the implementation which throws the exception in a static constructor:

public static bool HasFlags<T>(this T value, T flags) where T : struct
{
if (!(value is Enum))
{
throw new ArgumentException();
}
return EnumHelper<T>.HasFlags(value, flags);
}

private class EnumHelper<T> where T : struct
{
static EnumHelper()
{
if (!typeof(Enum).IsAssignableFrom(typeof(T))
{
throw new InvalidOperationException(); // Or something similar
}
}

internal static HasFlags(T value, T flags)
{
...
}
}

As Greco mentions, you could write the method in C++/CLI and then reference the class library from C# as another option.

Restricting the generic type parameter to System.Enum

You can use Jon Skeet's Unconstrained Melody project to do this.

Using Unconstrained Melody you would write:

class Generic<T> where T : IEnumConstraint

Which would accomplish the same thing.

More info about Unconstrained Melody with usage examples.

How to constraint a generic to be of type enum?

This is supported at the IL level but not in C#. You may take a look at unconstrained melody written by Jon Skeet which allows you to achieve that. And here's the corresponding blog post where he explains in details.

System.Enum as a generic type parameter with constraints

As Eric Lippert says that and I quote

ALL features are unimplemented until someone designs, specs, implements, tests, documents and ships the feature. So far, no one has done that for this one. There's no particularly unusual reason why not; we have lots of other things to do, limited budgets, and this one has never made it past the "wouldn't this be nice?" discussion in the language design team."


How to limit a generic type parameter to System.Enum

Unfortunately, you cannot - Microsoft closed this one out as a won't fix item.

You can treat enums as structs and use that as the constraint instead (I think that was how Jon Skeet did it in Unconstrained Melody?) but that is kind of unsightly.

Enum as Generic

Why not use Enum.Parse instead of the direct cast? Call ToString on the value to convert.

Generic Nullable Enum

Unfortunately there's no constraint available in C# that allows you to restrict that a given type is an enum. In IL there's such notion though. Jon blogged about it.

Using enum as generic type parameter in C#

No, it's not possible unfortunately. The best you can do is use where T : struct, IComparable, IConvertible, IFormattable (which of course is not the same). The interface restrictions are derived from the implementation of System.Enum.

Apart from that, you can check if typeof(T).IsEnum, which can detect the problem at runtime and presumably throw an exception. But there is no way to enforce this restriction at compile time.



Related Topics



Leave a reply



Submit