按名称获取类型 [英] Get Type by Name

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

问题描述

在我的代码,我想获得一个类型的名字。当我用一个字符串参数我失败了。然后我试图做在快速监视窗口中follwing:

In my code I am trying to get a type by name. When I was using a string argument I failed. Then I have tried to do the follwing in the Quick watch window:

Type.GetType(typeof(System.ServiceModel.NetNamedPipeBinding).Name)

返回null。为什么?以及如何通过名字获取所需的类型?

returns null. Why? and how to get the desired type by name?

推荐答案

Type.GetType 当你通过命名空间限定名称只能找到 mscorlib程序类型或当前装配。为了使它工作,你需要AssemblyQualifiedName。

Type.GetType can only find types in mscorlib or current assembly when you pass namespace qualified name. To make it work you need "AssemblyQualifiedName".

类型的程序集限定名称来获得。见
AssemblyQualifiedName。如果类型是在当前正在执行的
组件或在mscorlib.dll,它是足以提供的类型名称
。通过命名空间限定。

The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

类型.GetType

System.ServiceModel.NetNamedPipeBinding 住在System.ServiceModel.dll,因此 Type.GetType 找不到了。

System.ServiceModel.NetNamedPipeBinding lives in "System.ServiceModel.dll" hence Type.GetType can't find it.

这将工作

Type.GetType(typeof(System.ServiceModel.NetNamedPipeBinding).AssemblyQualifiedName)

或者,如果你知道大会已经使用下面的代码

Or if you know the assembly already use following code

assemblyOfThatType.GetType(fullName);//This just need namespace.TypeName

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

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