I have a dll named A.dll with strong name, another assembly B references A.dll. I place A.dll in c:\myapp, also A.dl开发者_如何学运维l is installed in GAC. I hope assembly B loads the A.dll from c:\myapp not GAC. But it always load the A.dll from GAC. My question is how to load a referenced dll from specified local location not GAC. Thanks.
According to the MSDN page about how the CLR loads assemblies, the CLR will load assemblies from the GAC before attempting to load assemblies from the working directory. There doesn't appear to be a way around this.
Alternatively, you could just not add the assembly to the GAC so that the CLR would be forced to find the assembly by probing.
You'll have to give it a different [AssemblyVersion]. Which ultimately makes sense, if the version numbers are the same then there's no reason that the GAC version wouldn't be good.
The only way is by using Assembly.LoadFile() to load your assembly. This allows you to specify a path to load.
There are several gotchas that make this very painful:
- You have to do Assembly.LoadFile() before the assembly is loaded automatically
- None of the dependencies of A.dll are loaded automatically, you will have to load all of them by hand
- A.dll would not be loaded into LoadFrom context (see msdn for more information), which effectively means any code that references A.dll cannot just use the types from A.dll, you must use reflection
It is possible to do this by hosting the CLR and implementing IHostAssemblyManager and IHostAssemblyStore. This is like using a bazooka to kill a mosquito though.
精彩评论