I am trying to implement dynamic loading of certain assemblies based on Environment.Is64BitProcess
.
This basically works like this:
AppDomain.AssemblyResolve
event
In the event handler load the assembly from the CPU type dependent sub path:
private Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
{
// args.Name is the display name of an assembly, e.g.:
// MyAssembly, Version=5.0.0.0, Culture=neutral, PublicKeyToken=abcdefghijklmn
if(!args.Name.Contains("MyAssembly"))
return null;
var path = Path.Combine(GetCpuTypeDependentPath(), "MyAssembly.dll");
return Assembly.LoadFrom(path);
}
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?
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;
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With