Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the relationship between a .NET assembly and CPU architecture?

What is the relationship between a .NET assembly and "bitness" or properly speaking, CPU architecture? I thought that .NET programs are compiled into CIL (bytecode) and that they might end up running in different architectures, and will be just-in-time compiled automatically. Thus there should be no "bitness" to a .NET assembly.

But the real world doesn't seem to be so simple. I test programs on a 64-bit machine and frequently run into weird compatibility issues between, say, my program and another .NET library. So my questions are:

  • Can a .NET binary somehow have a CPU architecture built into it?
  • Under what conditions does/should this happen?
  • On 64-bit machines, what chain of events results in .NET code being run in a 32-bit environment?

BTW: this question (Differences between 32 and 64-bit .NET (4) applications) and its top answer take for granted that there is a definite bitness for .NET applications. But I see no way of setting the architecture in Visual Studio.

like image 511
Adrian Ratnapala Avatar asked Oct 20 '22 20:10

Adrian Ratnapala


1 Answers

The question you reference takes this for granted because it is true: .NET applications are built specifically for different CPU architectures (and can also be built for "mixed platforms" in many cases, where it will run 64-bit if available and 32-bit otherwise).

By default this option is not clearly shown in Visual Studio, but the "Solution Platforms" dropdown can be added to the standard toolbar, by default alongside the "Solution configurations" dropdown.

Also, if you select "Configuration manager" from the configuration dropdown, you'll see your current platform, and can edit settings for each assembly in your solution.

In specific answer to your three bullets:

  • every .NET binary has a CPU architecture built into it, even if that is "Any CPU"
  • this happens for every build; it is non-optional
  • a 64-bit machine will run a .NET assembly as 32-bit if:
    • it was built as explicitly x86
    • the calling assembly was x86 (an "Any CPU" dll will run as 32-bit if the assembly referencing it is 32-bit)
like image 70
Dan Puzey Avatar answered Dec 18 '22 13:12

Dan Puzey