Despite reading a number of articles online, I am still confused about which platform target assemblies can be mixed with each other.
To test this, I created a Visual Studio Solution with three Console App projects and three .Net Framework class libraries.
The console app projects are AnyCPUApp, X86App, and X64App with the corresponding platform targets set in Configuration Manager and Project Properties > Build. The library projects are AnyCPULib, X86Lib, and X64Lib with the corresponding platform targets set in Configuration Manager and Project Properties > Build.
X86App will build and run with AnyCPULib and X86Lib. The compiler gives a "processor architecture mismatch" warning when X86App references X64Lib and it crashes with System.badImageFormatException if you try to run it.
X64App will build and run with AnyCPULib and X64Lib. The compiler gives a "processor architecture mismatch" warning when X64App references X86Lib and it crashes with System.badImageFormatException if you try to run it.
My questions:
- Why does AnyCPUApp run (with a "processor architecture mismatch" warning) against an X86Lib, but not against an X64Lib?
- Are there real-world cases where the processor architecture mismatch will cause a run-time failure?
- Is AnyCPU just meant for class libraries?
My xxxApp code looks like (change xxx to AnyCPU|X86|X64, uncomment clauses to test and update project references:
namespace xxxApp {
class Program {
static void Main(string[] args) {
//var anyCPULib = new AnyCPULib.AnyCPULib();
//Console.WriteLine(anyCPULib.Hello);
//System.Diagnostics.Debug.WriteLine(anyCPULib.Hello);
//var x86Lib = new X86Lib.X86Lib();
//Console.WriteLine(x86Lib.Hello);
//System.Diagnostics.Debug.WriteLine(x86Lib.Hello);
//var x64Lib = new X64Lib.X64Lib();
//Console.WriteLine(x64Lib.Hello);
//System.Diagnostics.Debug.WriteLine(x64Lib.Hello);
}
}
}
My xxxLib code looks like (change xxx to AnyCPU|X86|X64)
namespace AnyCPULib
{
public class AnyCPULib
{
public string Hello { get { return "Hello from AnyCPULib"; } }
}
}
I'm using Visual Studio 2019 Version 16.8.0 and .NET Framework 4.8