I've been trying to write a utility extension method for flagged enum values. The purpose with this method is to retrieve a list of all the flags that are currently enabled.
What I wanted to do was this:
public static IEnumerable<T> GetFlags<T>(this T value) where T:Enum
{
return Enum.GetValues(typeof(T)).OfType<Enum>().Where(value.HasFlag).Cast<T>();
}
However, since generics doesn't work with the Enum type I had to resort to the following:
public static IEnumerable<T> GetFlags<T>(this Enum value)
{
return Enum.GetValues(typeof(T)).OfType<Enum>().Where(value.HasFlag).Cast<T>();
}
Is there any way to get around this or should I resign myself to explicitly having to declare the type every time the method is called ?