人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

May 21, 2007

Assembly.Load() 系列方法

Filed under: Uncategorized

在.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.其好处:
  1. Skip assembly strong name verifications
  2. Skip CAS policy check
  3. Skip processor architecture loading rule
  4. Not execute any code in the target assembly, including module constructor
  5. Not apply any binding policy.
和execution assembly load APIs的比较:
  1. There is one inspection context per AppDomain. All the reflection only assemblies live in that context.
  2. Reflection only assemblies will be unloaded only when the AppDomain is unloaded, same as execution assemblies.
  3. 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.
  4. 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:
  5. 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.
  6. If ReflectionOnlyLoad() is called on an assembly when another assembly with that identity was already loaded, the already-loaded assembly will be returned.
  7. ReflectionOnlyAssemblyResolve event will be fired, instead of AssemblyResolve event.
  8. You have to return a reflection only assembly in ReflectionOnlyAssemblyResolve event handler.
  9. 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

Comments »

The URI to TrackBack this entry is: http://recordsome.blogsome.com/2007/05/21/p242/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham