vendredi 12 juin 2020

Casting an Object using a Type Variable [duplicate]

I am trying to convert an Object to its Type. We are receiving the type in variable below,

    public void TestFunction(object inputObject)
    {
        var type = inputObject.GetType();
        var inputTest = (type) inputObject;  // compilation error here
    }

How can this be resolved?

The following solutions are not working, since user still has to know the Type of class.

Casting a variable using a Type variable

cast object with a Type variable

How to cast Object to its actual type?





Aucun commentaire:

Enregistrer un commentaire