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

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

问题描述

在我的代码中,我试图按名称获取类型.当我使用字符串参数时,我失败了.然后我尝试在快速观看窗口中执行以下操作:

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)

返回空值.为什么?以及如何通过名称获取所需的类型?

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".

要获取的类型的程序集限定名称.看装配资格名称.如果类型在当前正在执行的程序集或 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.

参考 Type.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天全站免登陆