1

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)}");
                }
love2code
  • 441
  • 6
  • 9
  • This question [C# switch on type](https://stackoverflow.com/questions/4478464/c-sharp-switch-on-type) seems to be an exact duplicate with different options to solve a problem – Pavel Anikhouski Aug 11 '20 at 19:59

1 Answers1

6

Look at this case:

case Type EnumTypeA when EnumTypeA == typeof(T):

That will always be true (because you're switching on typeof(T)), and it's unrelated to the type called EnumTypeA at all. It's equivalent to:

case Type t when t == typeof(T):

What you actually want is:

case Type t when t == typeof(EnumTypeA):

So something like this:

switch (typeof(T))
{
    case Type t when t == typeof(EnumTypeA):
        Console.WriteLine("is EnumTypeA");
        break;
    case Type t when t == typeof(EnumTypeB):
        Console.WriteLine("is EnumTypeB");
        break;
    default:
        Type type = typeof(T);
        return new Exception($"Unsupported type {Type.GetTypeCode(type)}");
}

Personally I'd probably prefer to use if/else for this situation, or possibly a static Dictionary<Type, Action>, but it's hard to say without knowing more about the real scenario.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks that worked I was incorrectly using the case lines, there are more than 2 cases A and B. Actually I have 4, so I think the switch will be better. Not following about the Dictionary option though. – love2code Aug 11 '20 at 19:33
  • @love2code: You'd create a `Dictionary` with a delegate for each type of "what you want to do when presented with that type". The switch statement will definitely work - it just feels a *little* odd to use it matching in the same way each time, just with a different guard clause. – Jon Skeet Aug 11 '20 at 19:42
  • Yeah, i'm not a fan of this syntax `Type t when t == typeof(EnumTypeB)` , but not completely clear with the Dictionary yet, So with the dictionary i will have the enum type and a call back action, but how I can avoid the switch with that approach ? i will still need to know which is the enum right ? – love2code Aug 11 '20 at 22:09
  • Nevermind i see it here in this solution exposed by Pavel https://stackoverflow.com/questions/4478464/c-sharp-switch-on-type. Thanks both – love2code Aug 11 '20 at 23:28