Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit a generic type parameter to System.Enum [duplicate]

Possible Duplicates:
Anyone know a good workaround for the lack of an enum generic constraint?
Create Generic method constraining T to an Enum

Is is possible to limit the generic type parameter [I don't know if that's the right name] to an Enum?

For example how do I do something like this?

//VB.NET
Function GetValues(Of T As System.Enum)(ByVal value As T) As IEnumerable(Of T)
    Return [Enum].GetValues(value.GetType)
End Function

//C#
public IEnumerable<T> GetValues<T>(T value) where T : System.Enum
{
    return Enum.GetValues(value.GetType());
}

Update

I eventually used Jon Skeet's Unconstrained Melody for that purpose. Thanks to you all for your contributions.

like image 837
Alex Essilfie Avatar asked Feb 21 '11 15:02

Alex Essilfie


4 Answers

You can't. An alternative solution is using struct and run-time check.

public IEnumerable<T> GetValues<T>(T value) where T : struct {       if (!typeof(T).IsEnum) throw new NotSupportedException();     return (IEnumerable<T>)Enum.GetValues(value.GetType());  }  
like image 140
Cheng Chen Avatar answered Oct 04 '22 04:10

Cheng Chen


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.

like image 25
48klocs Avatar answered Oct 04 '22 06:10

48klocs


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.
like image 21
KeithS Avatar answered Oct 04 '22 05:10

KeithS


There is no need to make your method generic in this way.

You can just use the System.Enum as the type parameter in your return type:

using System.Linq;
.
.
.
public IEnumerable<Enum> GetValues(Enum value)
{
    return Enum.GetValues(value.GetType()).OfType<Enum>();
}
like image 21
Matt Ellen Avatar answered Oct 04 '22 06:10

Matt Ellen