当给定的部分限定的类型名称如何Type.GetType工作的?名称、类型、部分、工作

2023-09-02 10:26:55 作者:笑话往事.

在表格 FullTypeName,的AssemblyName ,即像 Type.AssemblyQualifiedName 只有没有版本,文化和公钥预选赛。

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.GetType 做这项工作,但很可惜,事实并非如此。下面code,例如,返回

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.