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

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

问题描述

我想获取动态对象的类型,例如:

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!

推荐答案

你需要这样做...

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

...对象没有被包装,原始代码工作正常.

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

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

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