原文:再谈CLR查找和加载程序集的方式

这是一个老问题,以前也有朋友写过一些文章介绍,但可能还不是很全面。我也多次被人问到,这里结合案例再次谈谈,希望对大家有所帮助。

本文范例代码可以通过这里下载 http://files.cnblogs.com/chenxizhang/AssemblyMatchDemoSolution.zip

根据程序集的特征,讨论这个问题,我们大致上有两个分类

没有做强名称签名的程序集

对于这种情况,CLR查找和加载程序集的方式如下

  1. 程序的根目录
  2. 根目录下面,与被引用程序集同名的子目录
  3. 根目录下面被明确定义为私有目录的子目录

同时,这种情况下,如果有定义codebase,则codebase的优先级最高,而且如果codebase指定的路径找不到,则直接报告错误,不再查找其他目录

有做强名称签名的程序集

对于这种情况,CLR查找和加载程序集的方式如下

  1. 全局程序集缓存
  2. 如果有定义codebase,则以codebase定义为准,如果codebase指定的路径找不到,则直接报告错误
  3. 程序的根目录
  4. 根目录下面,与被引用程序集同名的子目录
  5. 根目录下面被明确定义为私有目录的子目录

我们帮助大家更好地理解以上的说明,我准备用范例来做讲解。

1.准备基本范例

下面的范例演示了一个应用程序(MyApplication),和一个类库(MyLibrary) ,MyApplication是引用了MyLibrary的。

MyLibrary中有一个TestClass类型,提供了一个简单的方法(SayHello)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MyLibrary
{
public class TestClass
{
public void SayHello()
{
//这里为了演示方便,显示出来当前加载的程序集完整路径
Console.WriteLine(this.GetType().Assembly.Location);
Console.WriteLine("Hello,world");
}
}
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

在MyApplication中,我们就是简单地创建了这个类型的实例,然后调用方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
var c = new MyLibrary.TestClass();
c.SayHello(); Console.Read();
}
}
}

默认情况下,如果我们编译整个项目,那么MyLibrary.dll会被自动地复制到MyApplication的根目录,如下图所示

运行MyApplication.exe,我们能看到下面这样的输出

我们可以很清楚地看到,当前加载的MyLibrary.dll是来自于MyApplication的根目录的。

2. 假如我们不想将MyLibrary.dll放在应用程序的根目录

有时候,我们会希望单独存放MyLibrary.dll,那么第一种做法就是,直接在应用程序根目录下面建立一个与程序集同名的子目录,然后将程序集放进去。

我们注意到,根目录下面的MyLibrary.dll 被移动到了MyLibrary目录

然后,我们再次运行MyApplication.exe,能看到下面这样的输出:

3.假如我们有很多程序集,希望统一放在一个目录

第二步的方法虽然不错,但有一个问题,就是如果我们引用的程序集很多的话,就需要在根目录下面建立很多子目录。那么,有没有办法统一地将这些程序集放在一个目录中呢?

我们可以通过如下的方式,定义一个特殊的私有路径(PrivatePath)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="libs"></probing>
</assemblyBinding>
</runtime>
</configuration>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

同时,我们将MyLibrary.dll 移动到libs这个子目录下面去

然后,我们再次运行MyApplication.exe,能看到下面这样的输出:

这也就是说,对于没有签名的程序集,CLR一般会按照如下的规则查找和加载程序集

  1. 程序的根目录
  2. 根目录下面,与被引用程序集同名的子目录
  3. 根目录下面被明确定义为私有目录的子目录

但是,有一个例外

4. codebase的设置是优先的,而且是排他的

codebase是一个特殊的设置,我们可以在配置文件中明确地指定某个程序集的查找路径,这个规则具有最高的优先级,而且如果你做了设置,CLR就一定会按照你的设置去查找,如果找不到,它就报告失败,而不会继续查找其他路径。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="libs"/> <dependentAssembly>
<assemblyIdentity name="MyLibrary"
culture="neutral" />
<codeBase version="1.0.0.0"
href="CodeBase\MyLibrary.dll" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

请注意,我们保留了libs目录和Mylibrary目录,而且根目录下面也保留了一个MyLibrary.dll。 实际上,当前我们一共有4个dll. 那么到底会加载哪一个呢?

这种情况下,如果codebase下面找不到MyLibrary.dll 会怎么样呢?

我们发现他是会报告错误的,而不会查找其他目录的程序集。

5.如果有强名称签名会怎么样呢?

对程序集进行强名称签名的好处是,可以将其添加到全局全局程序集缓存中。这样既可以实现程序集的共享,又可以从一定程度上提高性能。

签名后,我们将其添加到全局程序集缓存中去

那么这种情况下,不管我们在应用程序根目录(或者下面的子目录)有没有MyLibrary.dll ,CLR都是尝试先从全局程序集缓存中查找和加载的。

需要注意的是,如果程序集是经过了强名称签名,则在定义codebase的时候,应该注明publicKeyToken

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="libs"/> <dependentAssembly>
<assemblyIdentity name="MyLibrary"
publicKeyToken="4a77fca346941a6c"
culture="neutral" />
<codeBase version="1.0.0.0"
href="CodeBase\MyLibrary.dll" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
 
 
 

总结

本文通过实例讲解了CLR在查找和加载程序集的时候所遵循的一些规则,针对有强名称和没有强名称的程序集,这些规则略有不同。本文范例代码可以通过这里下载 http://files.cnblogs.com/chenxizhang/AssemblyMatchDemoSolution.zip

相关问题

本文还没有涵盖到的另外两个特殊情况,在日常工作中不多见,大家有兴趣可以再找些资料研读。

1.在目录中查找的时候,如果dll查找不到,则会尝试查找同名的exe

2.如果程序集带有区域性,而不是语言中立的,则还会尝试查找以语言区域命名的子目录。

通常情况下,我们都就是程序集设置为语言中立的,所以不存在这个问题

 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

再谈CLR查找和加载程序集的方式的更多相关文章

  1. 【转】再谈CLR查找和加载程序集的方式

    这是一个老问题,以前也有朋友写过一些文章介绍,但可能还不是很全面.我也多次被人问到,这里结合案例再次谈谈,希望对大家有所帮助. 本文范例代码可以通过这里下载 http://files.cnblogs. ...

  2. CLR查找和加载程序集的方式

    C#开发者在开发WinForm程序.Asp.Net Web(MVC)程序等,不可避免的在项目中引用许多第三方的DLL程序集, 编译后引用的dll都放在根目录下.以我个人作品 AutoProject S ...

  3. CLR查找和加载程序集的方式(二) 流程图

    在前一篇文章<CLR查找和加载程序集的方式(一)>中详细介绍了CLR查找和加载程序的方式,分别介绍了配置与代码的实现方式. 本篇通过一个具体的流程图来帮助大家更加直观明了深入的掌握CLR查 ...

  4. CLR查找和加载程序集的方式(一)

    C#开发者在开发WinForm程序.Asp.Net Web(MVC)程序等,不可避免的在项目中引用许多第三方的DLL程序集, 编译后引用的dll都放在根目录下.以我个人作品 AutoProject S ...

  5. CLR查找和加载程序集 z

    C#开发者在开发WinForm程序.Asp.Net Web(MVC)程序等,不可避免的在项目中引用许多第三方的DLL程序集, 编译后引用的dll都放在根目录下.以我个人作品 AutoProject S ...

  6. linux系统——ld-linux.so.X查找和加载共享动态库的顺序

    ld-linux.so查找共享库的顺序: Glibc安装的库中有一个为ld-linux.so.X,其中X为一个数字,在不同的平台上名字也会不同.可以用ldd查看: #ldd /bin/cat linu ...

  7. CLR如何加载程序集以及程序集版本策略

    在项目的配置文件Web.config中,会看到<runtime>节点,以及包含在其中的<assemblyBinding>节点,这显然与程序集有关,这些节点到底何时被用到呢? 在 ...

  8. 五、CLR加载程序集代码时,JIT编译器对性能的产生的影响

    1.CLR首次加载代码造成的性能损失 四.CLR执行程序集中代码介绍了CLR在首次执行一个类的时,会初始化一个内部结构,然后当目标方法被首次调用时,JITComplier函数(JIT编译器)会验证IL ...

  9. .Net Core 通过依赖注入和动态加载程序集实现宿程序和接口实现类库完全解构

    网上很多.Net Core依赖注入的例子代码,例如再宿主程序中要这样写: services.AddTransient<Interface1, Class1>(); 其中Interface1 ...

随机推荐

  1. Django(part3)

    URLConf:负责url到view的map,就是一个urls.py module,通常在project和app级别都要定义, #mysite/urls.py from django.conf.url ...

  2. vue 初始化项目模板报错

    E:\xiaogezi.cn\vue>vue init webpack myProject vue-cli · Failed to download repo vuejs-templates/w ...

  3. (转载) 清理缓存 IPackageStatsObserver

    清理缓存 IPackageStatsObserver 2016-04-10 13:40 2288人阅读 评论(0) 收藏 举报  分类: android(59)  版权声明:本文为博主原创文章,未经博 ...

  4. Codeforces 986B. Petr and Permutations(没想到这道2250分的题这么简单,早知道就先做了)

    这题真的只能靠直觉了,我没法给出详细证明. 解题思路: 1.交换3n次或者7n+1次,一定会出现一个为奇数,另一个为偶数. 2.用最朴素的方法,将n个数字归位,计算交换次数. 3.判断交换次数是否与3 ...

  5. 自定义pulltoRefresh的刷新和加载动画

    一:定义刷新动画的layout 共同的设置方法是 1:getDefaultDrawableResId() 2:refreshingImpl() 正在刷新时的回调方法,可以设置开始动画 3:resetI ...

  6. 基本数据类型(int,bool,str)

    1.int bit_lenth() 计算整数在内存中占用的二进制码的长度 十进制 二进制 长度(bit_lenth()) 1 1 1 2 10 2 4 100 3 8 1000 4 16 10000 ...

  7. 15条JavaScript最佳实践【转】

    本文档整理大部分公认的.或者少有争议的JavaScript良好书写规范(Best Practice).一些显而易见的常识就不再论述(比如要用对象支持识别判断,而不是浏览器识别判断:比如不要嵌套太深). ...

  8. luogu P3414 SAC#1 - 组合数(组合数学)

    题意 求sigma(C(n,i))其中C是组合数(即C(n,i)表示n个物品无顺序选取i个的方案数),i取从0到n所有偶数. 由于答案可能很大,请输出答案对6662333的余数. (n<=101 ...

  9. pandas 7 合并 merge 水平合并,数据会变宽

    pd.merge( df1, df2, on=['key1', 'key2'], left_index=True, right_index=True, how=['left', 'right', 'o ...

  10. Android 获取麦克风的音量(分贝)

    基础知识 度量声音强度.大家最熟悉的单位就是分贝(decibel,缩写为dB).这是一个无纲量的相对单位.计算公式例如以下: 分子是測量值的声压,分母是參考值的声压(20微帕.人类所能听到的最小声压) ...