如何在 .Net Core 中使用自定义预处理器指令

编程入门 行业动态 更新时间:2024-10-21 06:09:57
本文介绍了如何在 .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

有谁知道如何使用 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 中使用自定义预处理器指令

本文发布于:2023-11-14 00:36:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585737.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   指令   如何在   Core   Net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!