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

查看:305
本文介绍了如何在.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

我正在根据<$ c $使用工具版本1.0.1 c> dotnet --version

有人知道如何使用.net core在命令行中正确设置指令吗?

Does anyone know how to properly set the directives from the command line using .net 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

并发布build

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天全站免登陆