• Assembly.Load,LoadFile,LoadFrom


    在加载Assembly的时候有三种Load Context可以选择:
    Load Context
    通常来说,当通过探测 GAC,host assembly store (if hosted), 或者 the ApplicationBase / PrivateBinPaths of the AppDomain来找到指定的Assembly时,被加载到Load Context中。
    LoadFrom Context
    通常来说,如果用户提供一个path来找到某个Assembly(并且在那个path中的Assembly没有在Load Context中找到),那么这个Assembly就被加载到LoadFrom Context中。有多个方法可以使用Path加载Assembly:LoadFrom(), CreateInstanceFrom(), ExecuteAssembly(), loading an assembly through interop using a codebase, etc.
    Neither Context
    If the user generated or found the assembly instead of Fusion, it's in neither context. This applies to assemblies loaded by Assembly.Load(byte[]) and Reflection Emit assemblies (that haven't been loaded from disk). Assembly.LoadFile() assemblies are also generally loaded into this context, even though a path is given (because it doesn't go through Fusion).
    注意: Assembly的三个方法并不与这三个Context严格对应,比如Assembly.Load(byte[]) 并不是用Load Context加载的。
    三种Context的比较可以参考下面的表格:
    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.


    Assembly.Load


    Assembly.LoadFrom

    Assembly.LoadFile

    Ref:
    http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx
    http://blogs.msdn.com/suzcook/archive/2003/09/19/57248.aspx
    http://blogs.msdn.com/suzcook/archive/2003/06/13/57180.aspx
  • 相关阅读:
    QT 界面刷新
    剑指 Offer 65. 不用加减乘除做加法
    3.nvidia-docker安装
    6.通过docker配置深度学习环境
    2.docker安装
    OSG+Visual Studio2015项目变量设置;
    Learning OSG programing---osgwindows
    日常学习网站推荐
    开发 MFC 应用的一般过程
    FVWM使用指南
  • 原文地址:https://www.cnblogs.com/whyandinside/p/1556391.html
Copyright © 2020-2023  润新知