What am I not understanding here?
I can require that T is an enum, but i can't cast a variable of type T to int.
public static List<int> EnumToInts<T>() where T : System.Enum
{ ... }
And I can do this:
T te = (T)Enum.Parse(typeof(T), name);
But if I attempt to cast te to an int
int ti = (int)te
I get
'Cannot convert type T to int'
this is ok
var e = Enum.Parse(typeof(T), name);
int i = (int)e;
but the cast to int here is not:
T te = (T)Enum.Parse(typeof(T), name);
int ti = (int)te;
What's wrong here? What don't I understand?
public static List<int> EnumToInts<T>() where T : System.Enum
{
List<int> l = new List<int>();
Type type = typeof(T);
string[] names = Enum.GetNames(type);
foreach (var name in names)
{
var e = Enum.Parse(type, name);
l.Add((int)e);
T te = (T)Enum.Parse(type, name);
l.Add((int)te);
}
return l;
}