get the Type for a object declared dynamic

39,412

Solution 1

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.

Solution 2

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

object tmp = Activator.CreateInstance(assembly, nmspace + "." + typeName);
Type unknown = tmp.GetType();
Share:
39,412
Radu M.
Author by

Radu M.

Updated on July 17, 2022

Comments

  • Radu M.
    Radu M. almost 2 years

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

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

    Except that in the above, GetType() returns the type of the wrapper for dynamic objects not the type of the wrapped object. Thanks!