This is a working solution for your problem:
Add 2 DLLs (x86 and x64) to your solution in a subfolder. Make them "Copy if new"
Specify the correct DLL that you use for development to debug from the 2 DLLs you added. Copy it Local = false.
What does this mean that when the application starts, the DLL does not load automatically. It will not load until you use the Type from this assembly. Once this happens, the event will be fired in .Net, which will ask where it can find your assembly.
Therefore, before using this assembly for the first time, make sure that you join this event.
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
In the contents of the handler, make sure that you load the DLL (x86 or x64) when it requests it.
static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { if (args.Name.Equals("MyFullAssemblyName")) { var path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); if (IntPtr.Size > 4) { var dll = System.IO.Path.Combine(path, @"MySubDir\MyDLL_x64.dll"); return System.Reflection.Assembly.LoadFile(dll); } else { var dll = System.IO.Path.Combine(path, @"MySubDir\MyDLL.dll"); return System.Reflection.Assembly.LoadFile(dll); } } return null; }
Voila. Now you can run the application in both 32-bit and 64-bit.
Alternatively, to add DLLs to a subfolder, you can make them as Embedded Resources, and then load them as follows:
static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { if (args.Name.Equals("MyFullAssemblyName")) { var ass = Assembly.GetExecutingAssembly(); if (IntPtr.Size > 4) { var strm = ass.GetManifestResourceStream("the.resource.name.for.MyDLL_x64.dll"); var data = new byte[strm.Length]; strm.Read(data, 0, data.Length); return Assembly.Load(data); } else { var strm = ass.GetManifestResourceStream("the.resource.name.for.MyDLL.dll"); var data = new byte[strm.Length]; strm.Read(data, 0, data.Length); return Assembly.Load(data); } } return null; }
This does not work for all builds. Some "hybrid" builds tend to fail if they are not loaded from disk (they can be resolved by writing them to disk before loading).