首页 > 解决方案 > C# 通过 Visual Studio 2019 将 .NET Core 3.0 编译为 Native Code

问题描述

我正在使用带有 C# 8.0 的 Visual Studio 2019,我的项目基于此示例:https ://github.com/dotnet/corert/tree/master/samples/HelloWorld

当我打开命令提示符并在我的解决方案文件夹中导航时,我使用以下命令编译本机代码:

dotnet 发布 -c 发布 -r win-x64

这很好用。

但是,在 Visual Studio 2019 中编译/构建(而不是使用 cmd)不会生成本机代码。在输出窗口中没有“生成本机代码”步骤。

这是我的 .csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <AssemblyVersion>1.0.1234</AssemblyVersion>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <Platforms>x64</Platforms>
    <DebugType>none</DebugType>
    <DebugSymbols>false</DebugSymbols>
    <LangVersion>8.0</LangVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.DotNet.ILCompiler" Version="1.0.0-alpha-*" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
    </ItemGroup>
</Project>

标签: .net-core.net-core-3.0corert

解决方案


您可以在构建后事件中发布它而无需重新编译 dotnet publish --no-build --no-restore /p:NativeLib=Shared -r win-x64 -c release


推荐阅读