我能否以引用“本地”程序集(不在 GAC 中)而不是 GAC 中的程序集的方式(Visual Studio 中的设置)配置 .NET 应用程序,尽管这两个程序集具有相同的名称和同一个版本?
最佳答案
如果两个程序集都具有强名称(已签名),则 CLR 将始终从 GAC 加载。
以下是运行时用于解析程序集引用的步骤(来自 How the Runtime Locates Assemblies ):
Determines the correct assembly version by examining applicable configuration files, including the application configuration file, publisher policy file, and machine configuration file. If the configuration file is located on a remote machine, the runtime must locate and download the application configuration file first.
Checks whether the assembly name has been bound to before and, if so, uses the previously loaded assembly. If a previous request to load the assembly failed, the request fails immediately without attempting to load the assembly.
Checks the global assembly cache. If the assembly is found there, the runtime uses this assembly.
Probes for the assembly (... some material omitted ...)
如后文所述:
There is no version checking for assemblies without strong names, nor does the runtime check in the global assembly cache for assemblies without strong names.
因此,如果您有能力从本地程序集中删除签名,应用程序将使用它而不是 GAC 中的签名。
有关运行时绑定(bind)机制的更多详细信息,请参阅 Suzanne Cook's blog .
This blog entry from Scott Hanselman还对绑定(bind)过程进行了很好的概述。
关于c# - 如何防止 .NET 应用程序从 GAC 加载/引用程序集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1606867/