我正在尝试在 .Net 核心中使用预处理器指令,但我无法确定设置指令的正确方法:
I am trying to use a preprocessor directive in .Net core, but I can't determine the correct way to get the directive to be set:
static void Main(string[] args) { Console.WriteLine("Hello World!"); #if MAC Console.WriteLine("MAC"); #else Console.WriteLine("NOT MAC"); #endif }我从命令行尝试了各种排列来使其工作,但我似乎遗漏了一些东西.这是我运行各种构建和运行命令时的 shell 输出:
I have tried various permutations from the command line to get this to work, but I seem to be missing something. Here is the shell output when I run various build and run commands:
~/dev/Temp/DirectiveTests $ dotnet msbuild /p:MAC=TRUE Microsoft (R) Build Engine version 15.1.548.43366 Copyright (C) Microsoft Corporation. All rights reserved. DirectiveTests -> /Users/me/dev/Temp/DirectiveTests/bin/Debug/netcoreapp1.1/DirectiveTests.dll ~/dev/Temp/DirectiveTests $ dotnet run /p:MAC=true Hello World! NOT MAC ~/dev/Temp/DirectiveTests $ dotnet run Hello World! NOT MAC我根据 dotnet --version
有谁知道如何使用 core 从命令行正确设置指令?
Does anyone know how to properly set the directives from the command line using core?
推荐答案你需要设置的是 /p:DefineConstants=MAC 注意这将覆盖项目中设置的常量,如 DEBUG 或 TRACE 可能被设置所以你可能使用的完整版本是
The thing you need to set is /p:DefineConstants=MAC note this will override constants set in the project like DEBUG or TRACE that may be set so the full version you would likely use would be
用于调试版本
dotnet msbuild /p:DefineConstants=TRACE;DEBUG;NETCOREAPP1_1;MAC /p:Configuration=Debug和发布版本
dotnet msbuild /p:DefineConstants=TRACE;NETCOREAPP1_1;MAC /p:Configuration=Release一个更简单的解决方案是创建一个名为 Mac 的配置,并且在你的 csproj 中有
An easier solution would create a configuration called Mac and in your csproj have
<PropertyGroup Condition="'$(Configuration)'=='Mac'"> <DefineConstants>TRACE;NETCOREAPP1_1;MAC</DefineConstants> </PropertyGroup>然后从命令行你只需要做
Then from the command line you just need to do
dotnet msbuild /p:Configuration=Mac更多推荐
如何在 .Net Core 中使用自定义预处理器指令
发布评论