Re-Triggering Assembly Resolve?

826 Views Asked by At

I'm triggering AssemblyResolve by setting "Copy Local" of a particular DLL to false. My AssemblyResolve gets triggered, and I get to choose the location of my DLL.

Later in the code, I want AssemblyResolve to re-trigger so I can specify a new DLL location, but since the DLL from the first AssemblyResolve was loaded successfully, I can't reload a new DLL.

Is there a way I can clear the current DLL and reload it? Or something like that???

Thanks!

3

There are 3 best solutions below

1
On BEST ANSWER

You have to use Assembly.LoadFile() to accomplish that. You can't do it with AssemblyResolve, the CLR very carefully avoids re-loading assemblies since that would open the door to mixing different versions of the same class. With some methods jitted against one version, some against another. Without any way to guarantee which, hilarity ensues.

LoadFile() is however a gun that shoots your foot and blows up in your face in very creative and hard to diagnose ways. One joy is that the exact same type is incompatible when loaded twice. You'd better rethink this.

6
On

How possibly can You imagine to do that in a running program? and more importantly why? Assembly represents working version of some executable code. Why do You want to have alternate realities at the same runtime. I fail to see the purpose, and that probably indicates some design flaw? You can get away with creating a separate ApplicationDomain in code and load Your App again with whatever assemblies You want.. but why?

luke

0
On

try like this:

string dllFile = "C:\\sample.dll";
Assembly asmLoader = Assembly.LoadFile(dllFile);
Type[] types = asmLoader.GetTypes();

Since all resources from the assembly cannot be reloaded/replaced while application is still @ runtime use LoadFile().