Matt's and Danny's answers both have half the answer. This should actually get you what you need:
public IEnumerable<T> GetValues<T>() where T : struct
{
if (!typeof(T).IsEnum) throw new InvalidOperationException("Generic type argument is not a System.Enum");
return Enum.GetValues(typeof(T)).OfType<T>();
}
Changes from Danny's answer:
- Though having a parameter of the generic type allows for type inference, since the value is not actually used it is more proper to explicitly specify the generic type (like with the Linq methods that don't take parameters).
- Enum.GetValues() returns an Array of Objects, which will not cast implicitly to an IEnumerable of T. The extra Linq method to cast the results (technically OfType is a filter operation but in this case it'll return everything) is necessary to conform to the return type.
- Optional: though NotSupportedException is as good a choice as any for an exception to throw, there are other options; ArgumentException, InvalidOperationException, InvalidCastException, etc. I chose InvalidOperationException because that's what it is; an invalid attempt to get enum values from a non-enum type. This is semantic and I'm not going to argue with anyone else's logic.