Search code examples
c#gac

Load assembly from Path


I need to load an assembly from a certain location and not from the GAC. I can not remove the assembly from the GAC, because my program has to run on different machines, where I couldn't control if the assembly is in the GAC or not.

I added a reference to the DLL-File to my project. Unfortuntely the assembly is loaded from the GAC.

I've tried the following code in my App.xaml.cs in the Startup-Event

var directory = Thread.GetDomain().BaseDirectory;
var dllPath = Path.Combine(directory, "MyAssembly.dll");
var assembly = Assembly.LoadFrom(dllPath);

Unfortunately the assembly is still loaded from the GAC.

What can I do to load it from the given location, even if the assembly is in GAC?


Solution

  • Unfortunately, you won't be able to bypass the GAC - Assembly.LoadFrom(), Assembly.LoadFile(), and even loading it into a byte array and using Assembly.Load(byte[]) will all check the GAC first for an assembly with the same identity, and load it instead.

    You used to be able to do what you want in .net 1.1, but since 2.0, the GAC is checked first. See How the Runtime Locates Assemblies - note that step 3 is check the GAC, before probing directories (even if you you fully specified it)

    If you want to load one up for Reflection (examining the assembly, rather than running it), you can use Assembly.ReflectionOnlyLoadFrom()