c# - Visual Studio 中的“首选 32 位”在控制台应用程序 (.NET Core) 下生成 64 位代码

标签 c# visual-studio .net-core

在 Visual Studio 2019 中创建简单的控制台 .NET Core 应用现在将默认使用 AnyCPU 平台(不设置 Prefer 32-bit,如 it was with a .NET Framework app )。

然而,如果切换到首选 32 位,结果将不再遵循旧逻辑,即生成 x86 可执行文件,而是生成 x64。

一些要检查的快速代码:

Console.WriteLine("Initially allocated: {0} bytes", AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize);
int noNumbers = 1000000;
object[] objectArray = new object[noNumbers];
Console.WriteLine("Allocated in the end: {0} bytes", AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize);

代码输出旁边的平台设置(红色)显示分配的内存量(橙色):

enter image description here

object[] 数组中的一个元素在 x86 平台上占用 4 个字节,在 x64 平台上占用 8 个字节。输出清楚地表明这是 x64 代码。 VMMap 还根据进程内可见的 64 位虚拟地址(绿色突出显示)证实了这一发现:

enter image description here

需要专门选择 x86 作为平台才能获得 32 位可执行文件:

enter image description here

在 Visual Studio 中将首选 32 位设置为启用时生成 64 位输出代码是否是有意更改?

在 Windows 10 x64 上的 Visual Studio 2019 16.5 上进行测试

最佳答案

正如 Hans Passant 的评论所指出的,Visual Studio 将构建(在其他文件中)一个 .exe - 它将完全忽略 Prefer 32-bit 设置,以及一个 .dll 将遵循设置(通过切换 32BITPREF)。

我联系了 Microsoft 支持人员,他们又与 Visual Studio 团队的人员进行了交谈。事实证明这是一个已知的事情,并且是一个 GitHub 问题 already exists用于完全删除 .NET Core 项目的Prefer 32-bit。事实上,Microsoft 支持将其归类为错误。

我还运行了一些测试,就目前而言,对于 .NET Core 控制台应用程序项目,Visual Studio 中的各种平台目标设置将确定 .exe 的以下结果> 和 .dll 分别:

enter image description here

围绕文件头和生成的进程位数的完整分析是 here .

关于c# - Visual Studio 中的“首选 32 位”在控制台应用程序 (.NET Core) 下生成 64 位代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60324529/

相关文章:

c# - ASP.NET MVC/C# - 如何使用 MVC 中数据表/列表的格式转换/创建 Excel (xls)?

c# - 解析 XML 文件时,将 Except() 与 IEnumerable<XElement> 一起使用不起作用

c# - 传递到 Microsoft.Build.Framework.ITaskItem 的多个项目。使用 clickOnce 发布时

visual-studio - Visual Studio 2017 msbuild与dotnet客户端工具不同

c# - Moq EF 6 DBSet,设置 FirstOrDefault 方法

c# while 中的两个语句

C++:我们的安装程序拒绝加载我们应用程序的资源 DLL(未找到模块)[仅限 XP]

c++ - 自 Visual Studio 2017 15.5.1 以来,_com_ptr_t 出现 Intellisense 错误

c# - 配置文件 'appsettings.json'未找到且不可选

c# - 序列化对象时如何忽略 JsonPropertyNameAttribute 注释?