如何在 Jetbrains Rider 中创建预处理器“定义”

问题描述 投票:0回答:2
  • 我正在使用 Jetbrains Rider 编写 C# 代码。
  • 我想根据特定情况(由我的预处理器常量指定)编译不同的行为。 这个想法是有这样的代码
#if MYCONSTANT
CallAGivenFunction();
#else
CallAnotherFunction();
#endif

所以,如果我想编译第一个分支(并被 Intellisense 看到),我将定义一个名为 MYCONSTANT 的预处理器常量;否则(什么都不做),我将编译第二个分支。

查看文档我了解到我应该在与我的 .csproj 相同的文件夹中创建一个名为 Directory.build.props 的文件,并且该文件应该有

<Project>
    <PropertyGroup>
        <DefineConstants>MYCONSTANT</DefineConstants>
    </PropertyGroup>
</Project>

但是在这样做之后,#if 子句中的代码

CallAGivenFunction();
仍然是灰色的(即 Rider 无法识别我定义的预处理器常量)。 我做错了什么?

c# intellisense preprocessor rider jetbrains-rider
2个回答
1
投票

在您的 .csproj 文件中,一些预处理器指令是使用您在 Directory.build.props 中使用的相同

<DefineConstants></DefineConstants>
元素定义的。 .csproj 中的元素会覆盖您在 Directory.build.props 中创建的任何内容 为了将您自己的常量考虑在内,您需要在 .csproj 中的元素之前加上
$(DefineConstants)
(例如)

<DefineConstants>$(DefineConstants);DEBUG;TRACE;</DefineConstants>

1
投票

您可以在项目的属性中声明预处理器。

#if TEST
private void test_function_a(int a)
{
}
#else
private void test_function_b(int b)
{
}
#endif

然后您可以进入项目的属性并添加一个名为 TEST 的预处理来使用,就像这样

© www.soinside.com 2019 - 2024. All rights reserved.