views:

878

answers:

2

I'm trying to load a few modules via hooking into the AppDomain.AssemblyResolve and AppDomain.ReflectionOnlyAssemblyResolve events. While I got the former to work, I fail miserably on the latter. I've boiled my problem down 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 fails with a FileNotFoundException when trying to Assembly.ReflectionOnlyLoad, but it doesn't call the ReflectionOnlyAssemblyResolve handler. I'm pretty stumped there.

Does anybody have an idea what could be the root cause of this and how to get this to work?

Thanks!

+3  A: 

It would appear 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

casperOne
@David Schmitt: Adding information to an answer should be done in your own answer, as per the FAQ: http://stackoverflow.com/questions/14593/etiquette-for-modifying-posts
casperOne
A: 

Expanding on casperOne's answer.

If you want to intercept direct Assembly Resolve events you need to hook into the AppDomain.AssemblyResolve event. This is a global hook though so it alone won't fit your scenario. However if your application is single threaded you could a short term hookup in order to intercept specific resolve 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 
}
JaredPar
AssemblyResolve is not triggered when using ReflectionOnlyLoad(). As I came to understand, this is by design, to avoid loading assemblies that are not "wanted" by the user of ReflectionOnlyLoad(). I'm not yet sure if I completely agree with that point of view though.
David Schmitt