Why is ReflectionOnlyAssemblyResolve not executing when trying Assembly.ReflectionOnlyLoad?

I am trying to load several modules by connecting to the AppDomain.AssemblyResolve and AppDomain.ReflectionOnlyAssemblyResolve events. While I made the former work, I fail at the latter. I cast my problem back to this little program:

 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; } } 

Running this program with a FileNotFoundException is FileNotFoundException when Assembly.ReflectionOnlyLoad attempted, but does not call the ReflectionOnlyAssemblyResolve handler. I'm pretty stumped.

Does anyone have any ideas what could be the main reason for this and how to make it work?

Thanks!

+6
c # clr assemblies
source share
2 answers

It seems that the ReflectionOnlyAssemblyResolve event is only used to resolve dependencies, not top-level assemblies, as indicated here:

http://codeidol.com/csharp/net-framework/Assemblies,-Loading,-and-Deployment/Assembly-Loading/

And here:

http://blogs.msdn.com/junfeng/archive/2004/08/24/219691.aspx

+8
source share

Extension of response to casperOne.

If you want to capture direct Assembly Resolve events, you need to hook into the AppDomain.AssemblyResolve event. This is a global hook, although it alone is not suitable for your scenario. However, if your application is single-threaded, you can use a short link to intercept specific permission events.

 static void LoadWithIntercept(string assemblyName) { var domain = AppDomain.CurrentDomain; domain.AssemblyResolve += MyInterceptMethod; try { Assembly.ReflectionOnlyLoad(assemblyName); } finally { domain.AssemblyResolve -= MyInterceptMethod; } } private static Assembly MyInterceptMethod(object sender, ResolveEventArgs e) { // do custom code here } 
0
source share

All Articles