How to cast object to type described by Type class?

前端 未结 5 597
旧时难觅i
旧时难觅i 2020-12-10 00:35

I have a object:

ExampleClass ex = new ExampleClass();

And:

Type TargetType

I would like to cast ex to ty

5条回答
  •  庸人自扰
    2020-12-10 01:36

    if (ex is ExampleClass) 
    {
      ExampleClass myObject = (ExampleClass)ex;
    }
    

    That would do it but I guess the question is what are you trying to achieve and why? I often find that if something seems really, really difficult then I'm probably doing it wrong.

提交回复
热议问题