简体   繁体   English

.NET程序集和CPU架构之间有什么关系?

[英]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? .NET程序集与“位数”或正确的CPU架构之间的关系是什么? 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. 我认为.NET程序被编译成CIL (字节码),并且它们可能最终在不同的体系结构中运行,并且将被及时编译。 Thus there should be no "bitness" to a .NET assembly. 因此,.NET程序集应该没有“位”。

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. 我在64位计算机上测试程序,并经常在我的程序和另一个.NET库之间遇到奇怪的兼容性问题。 So my questions are: 所以我的问题是:

  • Can a .NET binary somehow have a CPU architecture built into it? .NET二进制文件能否以某种方式内置CPU架构?
  • 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? 在64位计算机上,什么样的事件链导致.NET代码在32位环境中运行?

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. 顺便说一句:这个问题( 32位和64位.NET(4)应用程序之间的差异 )及其最佳答案理所当然地认为.NET应用程序有一定的重要性。 But I see no way of setting the architecture in Visual Studio. 但我认为无法在Visual Studio中设置架构。

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). 你提到的问题是理所当然的,因为它是真的:.NET应用程序是专为不同的CPU架构而构建的(在很多情况下也可以为“混合平台”构建,如果可用的话,它将运行64位,32-否则)。

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. 默认情况下,此选项未在Visual Studio中清晰显示,但“解决方案平台”下拉列表可以添加到标准工具栏,默认情况下与“解决方案配置”下拉列表一起添加。

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" 每个.NET二进制文件都内置了CPU架构,即使它是“任何CPU”
  • this happens for every build; 每次构建都会发生这种情况 it is non-optional 它是非选择性的
  • a 64-bit machine will run a .NET assembly as 32-bit if: 如果出现以下情况,64位计算机将以32位运行.NET程序集:
    • it was built as explicitly x86 它是显式构建的x86
    • the calling assembly was x86 (an "Any CPU" dll will run as 32-bit if the assembly referencing it is 32-bit) 调用程序集是x86(如果引用它的程序集是32位,“任何CPU”dll将以32位运行)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM