【发布时间】:2021-01-28 12:00:14
【问题描述】:
我正在尝试使用Directory.Build.props 文件为我的解决方案定义一个简单的预处理器常量。
出于测试目的,我创建了一个控制台应用程序 (C#),并在生成的 .csproj 文件旁边添加了一个 Directoy.Build.props 文件:
<Project>
<PropertyGroup>
<DefineConstants>TEST1</DefineConstants>
</PropertyGroup>
</Project>
我还手动编辑了 .csproj 本身来测试 <DefineConstants> 是否真的有效:
<DefineConstants>DEBUG;TRACE;TEST2;</DefineConstants>
现在是实际代码:
static void Main(string[] args)
{
#if TEST1
// NOT WORKING!
Console.WriteLine("<DefineConstants>TEST1</DefineConstants> in Directory.Build.props"); // NOT WORKING!
#else
Console.WriteLine("failed TEST1 in Directory.Build.props");
#endif
#if TEST2
// WORKS!
Console.WriteLine("<DefineConstants>TEST2</DefineConstants> in csproj"); // WORKS!
#else
Console.WriteLine("failed TEST2 in csproj");
#endif
}
那么为什么 MSBuild 无法在 Directory.Build.props 中找到我的常量?
【问题讨论】:
-
看看stackoverflow.com/questions/36793499/…,我猜这同样适用于 Directory.build.props。
-
@Karl-JohanSjögren 哦,哇!做到了!但是有没有办法在不手动编辑 csproj 的情况下自动发生?
-
使用
<DefineConstants>$(DefineConstants), ..</DefineConstants>可能会对您有所帮助
标签: c# .net visual-studio msbuild