在加载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的比较可以参考下面的表格:
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 |
|
|
LoadFrom |
|
|
Neither |
|
|
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
)
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
)
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
)
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
)
Assembly.LoadFile
public static Assembly LoadFile(
string path
)
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlEvidence)]
public static Assembly LoadFile(
string path,
Evidence securityEvidence
)
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