.net 以什么顺序搜索位置以加载引用的 DLL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:45:49  来源:igfitidea点击:

In what order are locations searched to load referenced DLLs?

.netdll

提问by Anthony Mastrean

I know that the .NET framework looks for referenced DLLs in several locations

我知道 .NET 框架会在多个位置查找引用的 DLL

  • Global assembly cache (GAC)
  • Any private paths added to the AppDomain
  • The current directory of the executing assembly
  • 全局程序集缓存 (GAC)
  • 添加到 AppDomain 的任何私有路径
  • 执行程序集的当前目录

What order are those locations searched? Is the search for a DLL ceased if a match is found or does it continue through all locations (and if so, how are conflicts resolved)?

搜索这些位置的顺序是什么?如果找到匹配项,是否会停止对 DLL 的搜索,还是会继续搜索所有位置(如果是,如何解决冲突)?

Also, please confirm or deny those locations and provide any other locations I have failed to mention.

另外,请确认或否认这些地点,并提供我没有提及的任何其他地点。

回答by Lars Truijens

Assembly loading is a rather elaborate process which depends on lots of different factors like configuration files, publisher policies, appdomain settings, CLR hosts, partial or full assembly names, etc.

程序集加载是一个相当复杂的过程,它取决于许多不同的因素,如配置文件、发布者策略、应用程序域设置、CLR 主机、部分或完整程序集名称等。

The simple version is that the GAC is first, then the private paths. %PATH% is never used.

简单的版本是首先是 GAC,然后是私有路径。%PATH% 从未使用过。

It is best to use Assembly Binding Log Viewer (Fuslogvw.exe)to debug any assembly loading problems.

最好使用程序集绑定日志查看器 (Fuslogvw.exe)来调试任何程序集加载问题。

EDIThttp://msdn.microsoft.com/en-us/library/aa720133.aspxexplains the process in more detail.

编辑http://msdn.microsoft.com/en-us/library/aa720133.aspx更详细地解释了该过程。

回答by Anthony Mastrean

I found an articlereferencing the MSDN article on DLL search orderthat says

我发现一篇文章引用了关于DLL 搜索顺序的 MSDN 文章,上面写着

For managed code dependencies, the Global Assembly Cache always prevails; the local assembly in application directory will not be picked up if there is an existing (or newer with policy) copy in the GAC.

对于托管代码依赖项,全局程序集缓存始终占优势;如果 GAC 中存在现有(或更新的策略)副本,则不会选取应用程序目录中的本地程序集。

Considering this, I guess the MSDN list is correct with one addition

考虑到这一点,我想 MSDN 列表是正确的,但添加了一个

0. Global assembly cache

回答by Anders Sandvig

"No longer is the current directory searched first when loading DLLs! This change was also made in Windows XP SP1. The default behavior now is to look in all the system locations first, then the current directory, and finally any user-defined paths."

“加载 DLL 时不再首先搜索当前目录!在 Windows XP SP1 中也进行了此更改。现在的默认行为是首先查看所有系统位置,然后是当前目录,最后是所有用户定义的路径。 ”

(ref. http://weblogs.asp.net/pwilson/archive/2003/06/24/9214.aspx)

(参考。 http://weblogs.asp.net/pwilson/archive/2003/06/24/9214.aspx

The default serach order, which can be changed by the application, is also described on MSDN: http://msdn.microsoft.com/en-us/library/ms682586.aspx

MSDN 上还描述了可以由应用程序更改的默认 serach 顺序: http://msdn.microsoft.com/en-us/library/ms682586.aspx