在.NET1.0中提供了
Assembly.Load(string assemblyString)
根据assembly的display name加载.
Assembly SampleAssembly = Assembly.Load(”SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3″);
Assembly.LoadFrom(string assemblyFile)
根据assembly所在的路径( 相对于当前目录)加载.
Assembly SampleAssembly = Assembly.LoadFrom(”c:\\Sample.Assembly.dll”);
在.NET1.1中又提供了
Assembly.LoadFile(string path)
LoadFile 与LoadFrom的比较:
LoadFile does not load files into the LoadForm context, and does not resolve dependencies using the load path,
LoadFrom cannot be used to load assemblies that have the same identities but different paths
在.NET 2.0中提供了
Assembly.ReflectionOnly属性(readonly)
指示assembly是否被加载到reflection-only context中
Assembly.ReflectionOnlyLoad(string assemblyString)
根据assembly的display name加载assembly到reflection-only context中.
string fullName = “System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″;
Assembly theAssembly = Assembly.ReflectionOnlyLoad( fullName );
Assembly.ReflectionOnlyLoadFrom(string assemblyFile)
根据assembly所在的路径( 相对于当前目录)加载assembly到reflection-only context中.
string file = “c:\\a.dll“;
Assembly theAssembly = Assembly.ReflectionOnlyLoadFrom( file );
注意:当assembly被加载到reflection-only context,只能做Reflection。可以读到它里面所有的Type,但不能
Create Instance.其好处:
- Skip assembly strong name verifications
- Skip CAS policy check
- Skip processor architecture loading rule
- Not execute any code in the target assembly, including module constructor
- Not apply any binding policy.
和execution assembly load APIs的比较:
- There is one inspection context per AppDomain. All the reflection only assemblies live in that context.
- Reflection only assemblies will be unloaded only when the AppDomain is unloaded, same as execution assemblies.
- CLR will not probe for dependencies. The user of those APIs is responsible to provide all the necessary assemblies using Reflection Only Assembly Load APIs. The reason of this decision is that by probing dependencies, CLR may return a different assembly than the one you want. And it will be very difficult to overwrite CLR’s decision. However, if an assembly with the same idemtity is already loaded in the inspection context, CLR will use it to satisfy the dependency.
- All reflection only assemblies will be cached. Only one assembly per identity is allowed in the inspection context. It does not matter how that assembly was loaded - the first one loaded always wins. This means:
- If someone attempts to load a second one using ReflectionOnlyLoadFrom(), it will fail with a FileLoadException. The decision is based on 3). If multiple assemblies are allowed to be loaded in inspection context, CLR will not know which assembly to be used when looking for dependencies.
- If ReflectionOnlyLoad() is called on an assembly when another assembly with that identity was already loaded, the already-loaded assembly will be returned.
- ReflectionOnlyAssemblyResolve event will be fired, instead of AssemblyResolve event.
- You have to return a reflection only assembly in ReflectionOnlyAssemblyResolve event handler.
- CLR will not return instances of custom attributes, since that means executing code of the target assembly. Instead, a new class CustomAttributeData will be used to return information about the custom attributes.
PRB:Assembly.LoadFrom 不加载与其位于同一目录的依赖程序集
http://support.microsoft.com/kb/327435/zh-cn
Reflection Only Assembly Loading
http://blogs.msdn.com/junfeng/archive/2004/08/24/219691.aspx
LoadFile vs. LoadFrom
http://blogs.msdn.com/suzcook/archive/2003/09/19/loadfile-vs-loadfrom.aspx