如何在 .Net Core 中使用自定义预处理器指令 [英] How to use custom preprocessor directives in .Net Core

查看:25
本文介绍了如何在 .Net Core 中使用自定义预处理器指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 .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

有谁知道如何使用 .net core 从命令行正确设置指令?

Does anyone know how to properly set the directives from the command line using .net core?

推荐答案

你需要设置的是 /p:DefineConstants=MAC 注意这将覆盖项目中设置的常量,如 DEBUGTRACE 可能被设置所以你可能使用的完整版本是

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 中使用自定义预处理器指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆