从MsBuild定义C#预处理程序 [英] Define C# preprocessor from MsBuild
问题描述
在我的C#
文件中,我希望具有这样的预处理条件:
In my C#
file I want to have a preprocessor condition like this:
#if DEMO
ShowSplash();
#endif
我正在从命令行运行此命令:
I'm running this command from command line:
MSBuild MySolution.sln /p:Configuration=Release /p:Platform="Any CPU" /p:DEMO=1
然后,在MyProject.csproj
文件中,我具有以下内容:
Then, in MyProject.csproj
file I have the following:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DefineConstants>TRACE;DEMO=$(DEMO)</DefineConstants>
</PropertyGroup>
但是预处理器似乎跳过了我的启动代码. (我知道"Any CPU"和"AnyCPU"之间的区别.我从没碰过它,所以我很确定Visual Studio不在乎空间.)
But the preprocessor seems to skip my splash code. (I'm aware of the difference between "Any CPU" and "AnyCPU". I never touched that, so I'm quite sure Visual Studio doesn't care about the space.)
DEMO
未定义?相同的构造似乎可以在其他项目类型中使用(例如.wixproj
)我在这里缺少什么?
DEMO
is not defined? The same construct seems to work in other project types (e.g. .wixproj
) What am I missing here?
推荐答案
首先,您应该只在以下位置定义(并在您的代码中进行测试)符号:DEMO
First, you should only define (and test in your code) the symbol, here: DEMO
然后,您应该有条件地将符号添加到现有符号(最终在项目属性中定义的符号):
Then, you should conditionally add your symbol to existing symbols (those eventualy defined in project's properties):
在.csproj
文件中的第一项<DefineConstants>
或创建另一个<PropertyGroup>
部分之后,添加以下行:
<DefineConstants Condition="'$(DEMO)'=='1'">$(DefineConstants);DEMO</DefineConstants>
In .csproj
file, after first item <DefineConstants>
or creating another <PropertyGroup>
section, add line:
<DefineConstants Condition="'$(DEMO)'=='1'">$(DefineConstants);DEMO</DefineConstants>
PS:这是一个经过测试的解决方案.
PS: this is a tested solution.
这篇关于从MsBuild定义C#预处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!