I have a generic function that receives a parameter type T which is forced to be an struct . I would like to know how do I check if that type of of certain Enum type declared, I'm doing something like this:
public static string GetSomething<T>() where T : struct
{
switch (typeof(T))
{
case Type EnumTypeA when EnumTypeA == typeof(T):
Console.WriteLine("is EnumTypeA");
break;
case Type EnumTypeB when EnumTypeB == typeof(T):
Console.WriteLine("is EnumTypeB");
break;
default:
Type type = typeof(T);
return new Exception($"Unsupported type {Type.GetTypeCode(type)}");
}
}
But i'm getting always EnumTypeA even when I send EnumTypeB
This is ideally what I would like to do:
switch (typeof(T))
{
case is EnumTypeA
Console.WriteLine("is EnumTypeA");
break;
case is EnumTypeB
Console.WriteLine("is EnumTypeB");
break;
default:
Type type = typeof(T);
return new Exception($"Unsupported type {Type.GetTypeCode(type)}");
}