之前在实现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. Binder学习笔记(十一)—— 智能指针

    轻量级指针 Binder的学习历程爬到驱动的半山腰明显感觉越来越陡峭,停下业务层的学习,补补基础层知识吧,这首当其冲的就是智能指针了,智能指针的影子在Android源码中随处可见.打开framewor ...

  2. upper_bound下确界

    //uppper_bound上确界找出首个大于某值的元素 #include<algorithm> #include<iostream> using namespace std; ...

  3. [hdu 2604] Queuing 递推 矩阵快速幂

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

  4. js对象的深浅拷贝

    JS数据类型可以分为(ES5,暂时不考虑ES6): 简单数据类型:Number.String.undefined.boolean 复杂数据类型:Object.Array 简单的数据类型,往往是赋值操作 ...

  5. Codeforces Round #521 (Div. 3) C. Good Array

    C. Good Array time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. 洛谷 P1036 选数

    嗯.... 这种类型的题在新手村出现还是比较正常的, 但是不知道为什么它的分类竟然是过程函数与递归!!!(难道这不是一个深搜题吗??? 好吧这就是一道深搜题,所以千万别被误导... 先看一下题目: 题 ...

  7. pytorch 检测图片中是否有人

    照搬pytorch官方代码,只是将数据集换成了INRIAPerson数据集中的train和test文件夹. 贴下代码和效果,代码是官方的,就不详细解释了. # License: BSD # Autho ...

  8. POJ1056 IMMEDIATE DECODABILITY & POJ3630 Phone List

    题目来源:http://poj.org/problem?id=1056   http://poj.org/problem?id=3630 两题非常类似,所以在这里一并做了. 1056题目大意: 如果一 ...

  9. light oj 1047 - Neighbor House(贪心)

    The people of Mohammadpur have decided to paint each of their houses red, green, or blue. They've al ...

  10. jQuery中animate()方法以及$('body').animate({"scrollTop":top})不被Firefox支持问题的解决

    $("body").animate({"scrollTop":top}): 只被chrome支持,而不被Firefox支持 $("html" ...