摘要:
严格上说这个是CLR loader的一个新冬冬。但我就厚颜无耻的借用了:)
System.Reflection.Assembly 类里加入了一些新的Method和Property。以下几个是这里讨论的:
Property Assembly.ReflectionOnly (http://lab.msdn.microsoft.com/library/en-us/cpref/html/P_System_Reflection_Assembly_ReflectionOnly.asp)
Method Assembly.ReflectionOnlyLoad(Byte[]) (http://lab.msdn.microsoft.com/library/en-us/cpref/html/M_System_Reflection_Assembly_ReflectionOnlyLoad_1_0d447871.asp)
Assembly.ReflectionOnlyLoad(String) (http://lab.msdn.microsoft.com/library/en-us/cpref/html/M_System_Reflection_Assembly_ReflectionOnlyLoad_1_62522ff1.asp)
Assembly.ReflectionOnlyLoadFrom(String) (http://lab.msdn.microsoft.com/library/en-us/cpref/html/M_System_Reflection_Assembly_ReflectionOnlyLoadFrom_1_62522ff1.asp)
从它们的名字就可以猜到,通过这些API装载的Assembly只能做Reflection。你可以读到它里面所有的Type,但你不能激活任何一个。对ReflectionOnly*返回的Type, CreateInstance会throw exception.
这些API有几个目的:
1.实现跨ProcessorArchitecture的introspection。比如说64位的Assembly是不能在32位的
.Net framework上装载的。如果你有一个64位的Assembly,在32位的
.Net framework上你就一点办法就没有了。而这些API可以让你看到里面的Type。这就让第二种情况成为可能。
2.Crosss ProcessorArchitecture Compilation。就是说用32位的Managed Tools去编译64位的Assembly。现在TlbExp/TlbImp用这些API来实现对64位managed assembly的操作。
3.Assembly.Load*很多时候会运行一些指令。在有些情况下你只想看Assembly中的Type,不想运行任何程序。这种时候你就可以使用这些新API。
4.另一个很重要的一点是现在Assembly.Load*都Apply Policy了。结果就是你拿到的和你想看的Assembly可能完全不一样。而Assembly.ReflectionOnlyLoad* 100%返回你指定的Assembly。这个对Compiler和Tool来说是很重要的。
它们的用法是在MSDN上有文档。下面是一些说明和限制:
· Duplicates: It's okay to load a copy of an assembly into the inspection context and another into the creating appdomain, for execution. They will result in different Assembly instances.
· Execution APIs: APIs which would execute something will throw an InvalidOperationException when given an inspection-only type. For example, CreateInstance(Type) should throw when passed a type from an inspection-only assembly.
·
· Caching: All inspection assemblies will......[
阅读全文]