AssemblyResolve not fired with anonymous handler

220 views Asked by At

I'm trying to resolve dynamically loaded assemblies, but the AssemblyResolve event does not appear to work with anonymous functions.

private void Load() {
    ResolveEventHandler resolve = (sender, args) => Assembly.LoadFile(pathToDependency);

    AppDomain.CurrentDomain.AssemblyResolve += resolve;
    AppDomain.CurrentDomain.AssemblyResolve += this._AssemblyResolve;
    Assembly.LoadFile(pathToDll);
}

private void _AssemblyResolve(Object sender, ResolveEventArgs args) {
    return Assembly.LoadFile(pathToDependency);
}

this._AssemblyResolve and resolve are virtually the same function, except that the anonymous resolve function creates a closure that uses a variable defined in the Load method.

For some reason, this._AssemblyResolve gets called, but resolve doesn't. Why?

I can't really rely on the _AssemblyResolve method because the method needs to know about a variable that is defined in the Load method. I can use a workaround, but I still don't understand why the anonymous function doesn't work.

0

There are 0 answers