Daniel Hilgarth
Daniel Hilgarth

Reputation: 174329

Assembly.LoadFrom with path and full assembly name

I am trying to implement dynamic loading of certain assemblies based on Environment.Is64BitProcess.
This basically works like this:

Now, this has the problem, that it doesn't check for the version, publicKeyToken etc of the loaded assembly.
What I would like to do now is to call Assembly.Load and simply supply an additional probing path. I know that this doesn't work as there is no such overload. Is there some other way to achieve my goal?

Upvotes: 8

Views: 2074

Answers (2)

Hans Passant
Hans Passant

Reputation: 941605

Just compare the properties of the assembly you found with the one that was requested. For example, a version check could look like this:

private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) {
    var name = new AssemblyName(args.Name);
    string path = System.IO.Path.Combine(IntPtr.Size == 8 ? "x64" : "x86", name.Name + ".dll");
    path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), path);
    var asm = Assembly.LoadFrom(path);
    var found = new AssemblyName(asm.FullName);
    if (name.Version != found.Version) throw new System.IO.FileNotFoundException(name.FullName);
    return asm;
}

Upvotes: 2

Nikola Radosavljević
Nikola Radosavljević

Reputation: 6911

Short of probing assemblies yourself I don't see that you can do this. You'd only have to fire up another AppDomain that will search for matching assembly, so that you can unload assemblies loaded during probing. Unfortunately

Upvotes: 1

Related Questions