Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 839
David Schmitt Avatar asked Feb 20 '09 15:02

David Schmitt


1 Answers

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

like image 138
casperOne Avatar answered Sep 22 '22 05:09

casperOne