我正在尝试使用“/p”参数设置要在调用 msbuild 期间编译的 C# 变量(字符串):
msbuild.exe mysolution.sln -p:MyCustomProperty=MyCustomValue
但是,我被困在如何在 C# 代码中访问这个“MyCustomProperty”值 (MyCustomValue)...我希望有一些简单的东西,比如使用这样的属性:
[GetAProjectPropertyAttribute("MyCustomProperty")]
public string MyVariable = "";
但不知道这样的属性是否存在?在编译过程中是否无法访问 C# 项目属性来设置某些代码语句?
如有任何帮助,我们将不胜感激!
也许有更好的解决方案,但如果你只有几个选项,你可以使用这个(标签适用于.NET MAUI项目)
csproj
<PropertyGroup Condition="'$(MyCustomProperty)'=='Value1'">
<DefineConstants>A</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(MyCustomProperty)'=='Value2'">
<DefineConstants>B</DefineConstants>
</PropertyGroup>
构建命令的参数
-p:MyCustomProperty=Value1
C#代码
#if A
//do something
#elif B
//do something else
#endif