David Schmitt
David Schmitt

Reputation: 59355

Why is ReflectionOnlyAssemblyResolve not executed when trying to Assembly.ReflectionOnlyLoad?

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!

Upvotes: 8

Views: 9949

Answers (3)

CSharpie
CSharpie

Reputation: 9477

You can force it by calling ExportedTypes on the assembly like this:

var dummy = asm.ExportedTypes;

So if you want to load an assembly with all its referecnes recursively:

 private void _forceAssemblyResolve(Assembly asm) { var dummy = asm.ExportedTypes; }

 var result= Assembly.ReflectionOnlyLoad("Foo");
 _forceAssemblyResolve(result);

public static Assembly ReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args)
{
    var childAssembly = _resolve(args);
    _forceAssemblyResolve(childAssembly);
}

Upvotes: 0

casperOne
casperOne

Reputation: 74560

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

Upvotes: 8

JaredPar
JaredPar

Reputation: 755269

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 
}

Upvotes: 1

Related Questions