获得类型的对象声明动态 [英] get the Type for a object declared dynamic

查看:125
本文介绍了获得类型的对象声明动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得的类型为动态对象,是这样的:

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

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

除在上面,的GetType()返回包装为动态对象不被包装的对象的类型的类型。谢谢!

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

推荐答案

您需要这样做......

You need to do this...

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

顺便说一句,这是一个有点混乱,因为如果你在你目前的组装调用Activator.CreateInstance一个类型...

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))

...对象不裹和原来的code正常工作。

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

这篇关于获得类型的对象声明动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆