如何合并命令行常量和配置常量

Tro*_*ers 3 msbuild

我有一个项目,其中为配置定义了预处理器常量

<DefineConstants>TRACE;DEBUG;VAR1</DefineConstants>
Run Code Online (Sandbox Code Playgroud)

我的代码可能是:

#if VAR1
        Console.WriteLine("Var1");
#endif
#if VAR2
        Console.WriteLine("Var2");
#endif
Run Code Online (Sandbox Code Playgroud)

我想执行 MSBuild 并定义常量 VAR2,而不覆盖配置中定义的常量

我尝试:

MSBuild MyProject.csproj /p:DefineConstants=VAR2
Run Code Online (Sandbox Code Playgroud)

但项目文件中定义的常量未设置:我的程序仅显示

变量2

我尝试过类似的事情但没有成功:

MSBuild MyProject.csproj /p:DefineConstants=$(DefineConstants);VAR2
Run Code Online (Sandbox Code Playgroud)

或者

<DefineConstants>$(DefineConstants);TRACE;DEBUG;VAR1</DefineConstants>
Run Code Online (Sandbox Code Playgroud)

这是一种合并项目中定义的常量和命令行上定义的常量的方法吗?

小智 9

据我所知,通过命令行定义的属性将始终覆盖具有相同名称的静态定义的属性。<DefineConstants />作为解决方法,您可以将项目文件中未定义的附加属性加入队列:

<DefineConstants>TRACE;DEBUG;VAR1;$(MyCommandLineConstants)</DefineConstants>
Run Code Online (Sandbox Code Playgroud)

并以这种方式调用 MSBuild:

MSBuild MyProject.csproj /p:MyCommandLineConstants=VAR2;VAR3
Run Code Online (Sandbox Code Playgroud)