之前在实现Autofac扫描自加载程序集实现IOC时候遇到程序集依赖的问题,在网上搜了一下,没有发现中文世界的相关描述。随取google拿了几篇文章,翻译&自己的理解,之后会写一些小demo,如下:

相关原文:

http://stackoverflow.com/questions/1477843/difference-between-loadfile-and-loadfrom-with-net-assemblies

http://blogs.msdn.com/b/suzcook/archive/2003/07/21/57232.aspx

http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57143.aspx

1、程序集标示

程序集加载有两种标示程序集的策略:绑定时和绑定后。标示用来探测一个程序集是否和另一程序集完全相同,或者是否是另外一个程序集所要引用的。

绑定时:用程序集名称来探测是否相同。对非强命名程序集会忽略版本号;可以不用详细指明程序集全名,但是建议总是给出全名,否则就是自己找麻烦。

绑定后即已经加载的程序集:程序集的manifest文件路径是用来探测的标示。对于两个相同的程序集,如果他们的加载路径不同,即使他们内部有完全相同的类型,也不能相互转换。

对于 Load(byte[]) 或者 created by Reflection Emit加载的程序集,总是被判断为不同的程序集,即使他们从二进制上来说完全相同。

2、程序集绑定上下文

有三种绑定上下文:Load、Load From、其他:

Load context

从GAC、宿主目录(如IIS Cache)、或者ApplicationBase / PrivateBinPaths加载的程序集.

Load From context

通常,用户提供一个不能被加载到Load上下文的路径来加载程序集, 可通过 LoadFrom(), CreateInstanceFrom(), ExecuteAssembly()等方法来加载成Load From上下文。

Neither context

非以上两种,通过 Assembly.Load(byte[]) 或者Reflection Emit assemblies或者Assembly.LoadFile()加载的。

推荐使用Load上下文,当然另外两种在某些情景下也很有用。

 Title

Advantages

Disadvantages

Load

  • Gets the benefits of versioning and policy.
  • Best avoids "Dll Hell." Dll Hell can break your app over time.
  • Dependencies available in the Load context are automatically found.
  • Dependencies in other contexts are not available unless you subscribe to the AppDomain.AssemblyResolve event.

LoadFrom

  • Assemblies can be loaded from multiple paths, not just from beneath the ApplicationBase.
  • Dependencies already loaded in this context will automatically be found.
  • Dependencies in the same dir as the requesting LoadFrom context assembly will automatically be found.
  • If a Load context assembly tries to load this assembly by display name, it will fail to be found by default (e.g., when mscorlib.dll deserializes this assembly).
  • Worse, an assembly with the same identity but at a different path could be found on the probing path, causing an InvalidCastException, MissingMethodException, or unexpected method behavior later on.
  • If an assembly by the same identity is already loaded, you'll get that one back, even if you've specified a path to a different one.
  • It does a FileIOPermission.Read + PathDiscovery demand, or a WebPermission demand on the path/URL specified.
  • The ngen image (if any) won't be used.
  • Can't be loaded as domain-neutral.
  • v1.0 and v1.1 CLR only: won't be affected by policy, so can't be centrally serviced.

Neither

  • Avoids probing costs for loading this assembly.
  • You can load multiple assemblies with the same identity into the same appdomain.
  • You get the bits from the location you specified (as opposed to LoadFrom). Starting in v2, policy/GAC overrides it, however.
  • Nothing can bind to this assembly unless you've subscribed to the AssemblyResolve event.
  • Dependencies can only be loaded from the Load context or using the AssemblyResolve event.
  • Passing this assembly to another AppDomain may become tricky (e.g., when this is a Reflection Emit assembly that hasn't been saved).
  • The ngen image (if any) won't be used.
  • Can't be loaded as domain-neutral.
  • v1.0 and v1.1 CLR only: won't be affected by policy, so can't be centrally serviced.

.net程序集标示与绑定上下文的更多相关文章

  1. knockoutJS学习笔记07:绑定上下文

    所谓绑定上下文就是当前绑定(dat-bind)所使用到的对象(ViewModel).在单个对象绑定的情况下是很容易理解的,但对象可能是复杂的类型,嵌套很多层,这个时候每层都有自己的上下文对象,理解起来 ...

  2. 【Knockout】四、绑定上下文

    Binding context binding context是一个保存数据的对象,你可以在你的绑定中引用它.当应用绑定的时候,knockout自动创建和管理binding context的继承关系. ...

  3. .NET 的程序集加载上下文

    原文:.NET 的程序集加载上下文 我们编写的 .NET 应用程序会使用到各种各样的依赖库.我们都知道 CLR 会在一些路径下帮助我们程序找到依赖,但如果我们需要手动控制程序集加载路径的话,需要了解程 ...

  4. .Net dll多个同名的程序集版本冲突共存与通过基本代码或探测定位程序集方案

    .Net dll多个同名的程序集版本冲突共存与通过基本代码或探测定位程序集方案 在使用调用程序集的引用中的信息和配置文件中的信息确定了正确的程序集版本之后,并且在公共语言运行时在全局程序集缓存中进行检 ...

  5. .Net Core 为 x86 和 x64 程序集编写 AnyCPU 包装

    前言 这几天研究了一下 vJoy 这个虚拟游戏手柄驱动,感觉挺好玩的.但是使用时发现一个问题,C# SDK 中的程序集被分为 x86 和 x64 两个版本,如果直接在 AnyCPU 平台编译运行就有隐 ...

  6. ASP.NET MVC Model绑定(四)

    ASP.NET MVC Model绑定(四) 前言 前面的篇幅对于Model绑定器IModelBinder以及实现类型.Model绑定器提供程序都作了粗略的讲解,可以把Model绑定器想象成一个大的容 ...

  7. ASP.NET MVC Model绑定(一)

    ASP.NET MVC Model绑定(一) 前言 ModelMetadata系列的结束了,从本篇开始就进入Model绑定部分了,这个系列阅读过后你会对Model绑定有个比较清楚的了解, 本篇对于Mo ...

  8. 【WPF】绑定数据

    WPF绑定数据 模型类(继承 INotifyPropertyChanged,实现属性的变更通知)

  9. KnockoutJS 3.X API 第四章 表单绑定(6) click绑定

    目的 click绑定主要作用是用于DOM元素被点击时调用相关JS函数.最常见用于button.input.a元素. 例如: You've clicked timesClick me var viewM ...

随机推荐

  1. 项目管理之码云和git

    目录 学习链接 1 码云 1 第一步,注册 2 第二部,登录 2 创建项目 2 git管理 4 如何生成公钥 5 clone项目 5 提交项目 6 1.本地初始化一个项目 6 2.开始第一次上传你的项 ...

  2. P2253 好一个一中腰鼓!

    题意:给你一个序列,初始是0,每次一个操作,把一个数^=1 每次求出最长01串的长度 正解:线段树(虽然暴力能过) 对于每个区间,记录三个值 lmax,以l为首的01串长度 rmax,以r为尾的01串 ...

  3. C++基础学习10:继承

    继承是类与类之间的关系,是一个很简单很直观的概念,与现实世界中的继承(例如儿子继承父亲财产)类似. 继承可以理解为一个类从另一个类获取方法(函数)和属性(成员变量)的过程.如果类B继承于类A,那么B就 ...

  4. (转)TestNG框架提供两种传入参数的方法:

    1.从testng.xml传入参数. 如果参数是一些简单的值,可以直接在testng.xml中定义.这也是最常用的一种. 可以在测试用例中给参数一个默认值.这样,即使在xml文件中没有这个变量,你的测 ...

  5. maven mirrorOf

    转载 http://blog.csdn.net/isea533/article/details/21560089 在maven中配置一个mirror时,mirror通常会设置成*,还有可能是一个具体的 ...

  6. hdu1686 KMP 求在字符串A中字符串B出现的次数

    Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  7. arraylist和linkedlist内部的实现大致是怎样的

    1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,ArrayList优于LinkedList,因为ArrayList可以随 ...

  8. POI 读大文件日志

    POI的三个目录 usermodel 包含很多类,方便用户使用,但是占用内存大 eventusermodel 使用xml的SAX事件解析,XSSFReader创建时必须使用OPCPackage,pkg ...

  9. ubuntu下找不到eth0

    1.,查找不到eth0 2.修改/etc/network/interface 发现没有eth0网卡信息 添加如下 autho eth0 iface eth0 inet dhcp 执行 sudo /et ...

  10. JS判断所有IE浏览器所有版本

    原来判断IE浏览器版本很简单,但是随着版本的升级,navigator.userAgent显示的信息也不一样:下图是IE11显示的信息