cast object with a Type variable

前端 未结 5 1964
暗喜
暗喜 2020-11-30 10:59

The following doesn\'t work, of course. Is there a possible way, which is pretty similar like this?

Type newObjectType = typeof(MyClass);

var newObject = gi         


        
5条回答
  •  广开言路
    2020-11-30 11:38

    Maybe you can solve this using generics.

    public void CastToMyType(object givenObject) where T : class
    {
       var newObject = givenObject as T;
    }
    

提交回复
热议问题