mardi 9 novembre 2021

C#10 - using the type T to switch in a generic method

There are lots of answers on how to switch on type in older versions of C#. My question has to do with how to switch on the type called out in a generic method:

    public T GetAxis<T>(object axisObject)
    {
        switch (typeof(T))
        {
            case Axis:
                //...do something
                break;
        }

        return null;
    }

I am getting an error on the line:

case Axis axis:

The error is:

The expression of type 'System.Type' cannot be handled by a pattern of type 'MyNamespace.Axis'.

I believe my question is how do I get the Type being called out in the <> specification so I can switch on it.





Aucun commentaire:

Enregistrer un commentaire