There are several ways to handle this. The first one is simple, acknowledge the fact that your application really has a dependency on the target architecture. And that AnyCPU is not a proper setting. Very rarely, the huge memory space of the virtual address that you get from x64 is required, especially since you also need and need to make it work on x86. So set the target of the EXE platform to x86, and you're done.
Secondly, this means that itβs just a deployment problem. All you have to do is copy the x64 assembly of the mixed mode assembly when your application is installed on a 64-bit operating system and x86 on a 32-bit operating system. You will need to create an installation project that takes care of this. The easiest way is to create two of them. Also the only way afaik.
Thirdly, the one who has the bells, where he works in any case, the one that you undoubtedly ask. This requires changes to the code, project, and installer. What you need to do is write a post-build event that will create two subdirectories with names like "x86" and "x64". And copy the corresponding version of the DLL into them. This ensures that the CLR cannot find these assemblies.
In your code, you must write an event handler for the AppDomain.CurrentDomain.AssemblyResolve event. Sign it in your Main () method before trying to use any types from the assembly. An event handler should use Assembly.LoadFrom () to load the correct assembly from a subdirectory based on the value of IntPtr.Size. This is 8 when you run 64-bit mode.
I should mention another approach, but usually frowned at SO. Install both assemblies in the GAC. Everything is automatic.
source share