当给定部分限定的类型名称时 Type.GetType 如何工作? [英] How Type.GetType works when given partially qualified type name?
问题描述
在很多地方,我遇到过 FullTypeName, AssemblyName
形式的部分限定类型名称,即像 Type.AssemblyQualifiedName
一样,但没有版本、文化和 publicKeyToken 限定符.
In numerous places do I encounter partially qualified type names of the form FullTypeName, AssemblyName
, i.e. like Type.AssemblyQualifiedName
only without the version, culture and publicKeyToken qualifiers.
我的问题是如何才能以最少的努力将其转换为相应的Type
?我认为 Type.GetType
可以完成这项工作,但遗憾的是,它没有.例如,以下代码返回 null
:
My question is how can one convert it to the respective Type
in a minimum of effort? I thought that Type.GetType
does the job, but alas, it does not. The following code, for instance, returns null
:
Type.GetType("System.Net.Sockets.SocketException, System");
当然,如果我指定完全限定名称,它确实有效:
Of course, if I specify the fully qualified name it does work:
Type.GetType("System.Net.Sockets.SocketException, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
非常感谢.
推荐答案
如果它所在的 DLL 还没有加载到应用程序域中(例如你使用过它),你需要像这样的完整路径,如果它已经加载了,它可以用较短的版本找到它.
If the DLL it's in isn't already loaded into the application domain (e.g. you used it), you need the full path like this, if it's already loaded, it can find it with the shorter version.
回答你的问题:第二个版本总是有效,坚持下去,你有一种担心的方式.
To answer your question: the second version always works, stick with it and you have one way to worry about.
这篇关于当给定部分限定的类型名称时 Type.GetType 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!