按名称获取类型 [英] Get Type by Name
问题描述
在我的代码,我想获得一个类型的名字。当我用一个字符串参数我失败了。然后我试图做在快速监视窗口中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.
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屋!