我正在尝试通过挂钩AppDomain.AssemblyResolve
和AppDomain.ReflectionOnlyAssemblyResolve
事件来加载一些模块.虽然我让前者工作,但我对后者失败了.我把问题归结为这个小程序:
public static class AssemblyLoader { static void Main(string[] args) { AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += ReflectionOnlyAssemblyResolve; // fails with FileNotFoundException Assembly.ReflectionOnlyLoad("Foo"); } public static Assembly ReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args) { Trace.TraceInformation( "Failed resolving Assembly {0} for reflection", args.Name); return null; } }
运行此程序失败,FileNotFoundException
尝试时Assembly.ReflectionOnlyLoad
,但它不会调用ReflectionOnlyAssemblyResolve处理程序.我很难过.
有没有人知道这可能是什么原因以及如何让它发挥作用?
谢谢!
看起来ReflectionOnlyAssemblyResolve事件仅用于解析依赖关系,而不是顶级程序集,如下所示:
http://codeidol.com/csharp/net-framework/Assemblies,-Loading,-and-Deployment/Assembly-Loading/
和这里:
http://blogs.msdn.com/junfeng/archive/2004/08/24/219691.aspx