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

Adr*_*ala 5 .net 32bit-64bit visual-studio

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?
  • 在什么条件下/应该发生这种情况?
  • 在 64 位机器上,什么事件链会导致 .NET 代码在 32 位环境中运行?

顺便说一句:这个问题(32 位和 64 位 .NET (4) 应用程序之间的差异)及其最佳答案理所当然地认为 .NET 应用程序有一定的重要性。但是我看不到在 Visual Studio 中设置架构的方法。

Dan*_*zey 3

您引用的问题认为这是理所当然的,因为这是事实:.NET 应用程序是专门为不同的 CPU 架构构建的(并且在许多情况下也可以为“混合平台”构建,其中它将运行 64 位(如果可用)和 32 位否则有点)。

默认情况下,此选项不会在 Visual Studio 中清晰显示,但可以将“解决方案平台”下拉列表添加到标准工具栏,默认情况下位于“解决方案配置”下拉列表旁边。

此外,如果您从配置下拉列表中选择“配置管理器”,您将看到当前平台,并且可以编辑解决方案中每个程序集的设置。

具体回答你的三个要点:

  • 每个 .NET 二进制文件都内置了一个 CPU 架构,即使是“任何 CPU”
  • 每个构建都会发生这种情况;它是非可选的
  • 如果满足以下条件,64 位计算机将作为 32 位运行 .NET 程序集:
    • 它明确地构建为 x86
    • 调用程序集是 x86(如果引用它的程序集是 32 位,则“任何 CPU”dll 将以 32 位运行)