get the Type for a object declared dynamic

后端 未结 2 1998
太阳男子
太阳男子 2020-12-01 10:57

I would like to get the Type for an dynamic object, something like:

dynamic tmp = Activator.CreateInstance(assembly, nmspace + \".\" + typeName);
Type unknow         


        
相关标签:
2条回答
  • 2020-12-01 11:03

    If you can use Activator.CreateInstance, you can directly use:

    object tmp = Activator.CreateInstance(assembly, nmspace + "." + typeName);
    Type unknown = tmp.GetType();
    
    0 讨论(0)
  • 2020-12-01 11:19

    You need to do this...

    Type unknown = ((ObjectHandle)tmp).Unwrap().GetType();
    

    By the way, this is a little confusing because if you call Activator.CreateInstance on a type in your current assembly...

    Activator.CreateInstance(typeof(Foo))
    

    ...the object is not wrapped and the original code works fine.

    0 讨论(0)
提交回复
热议问题