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的比较可以参考下面的表格:
AdvantagesDisadvantages
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.


ContractedBlock.gif ExpandedBlockStart.gif Assembly.Load
public static Assembly Load(
   
string assemblyString
)
public static Assembly Load(
   
byte[] rawAssembly  //Assembly的二进制形式
)
public static Assembly Load(
    AssemblyName assemblyRef
)
public static Assembly Load(
   
byte[] rawAssembly, //Assembly的二进制形式
    byte[] rawSymbolStore //Assembly Symbol的二进制形式
)
public static Assembly Load(
    AssemblyName assemblyRef,
    Evidence assemblySecurity
)
public static Assembly Load(
   
string assemblyString,
    Evidence assemblySecurity
)
[SecurityPermissionAttribute(SecurityAction.Demand, Flags
= SecurityPermissionFlag.ControlEvidence)]
public static Assembly Load(
   
byte[] rawAssembly,
   
byte[] rawSymbolStore,
    Evidence securityEvidence
)


ContractedBlock.gif ExpandedBlockStart.gif Assembly.LoadFrom
public static Assembly LoadFrom(
   
string assemblyFile
)
public static Assembly LoadFrom(
   
string assemblyFile,
    Evidence securityEvidence
)
public static Assembly LoadFrom(
   
string assemblyFile,
    Evidence securityEvidence,
   
byte[] hashValue,
    AssemblyHashAlgorithm hashAlgorithm
)

ContractedBlock.gif ExpandedBlockStart.gif Assembly.LoadFile
public static Assembly LoadFile(
   
string path
)

[SecurityPermissionAttribute(SecurityAction.Demand, Flags
= SecurityPermissionFlag.ControlEvidence)]
public static Assembly LoadFile(
   
string path,
    Evidence securityEvidence
)

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

转载于:https://www.cnblogs.com/whyandinside/archive/2009/08/29/1556391.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值