![](/img/trans.png)
[英]How do I test for compiler directives with an MSBuild Condition in a .csproj file?
[英]I want to create ChooseElement for .csproj with MSBuild How?
我想創建類似下面的東西來以編程方式創建 .csproj 文件,我使用
例如 :
<Choose>
<When Condition=" '$(Configuration)'=='debug' ">
<PropertyGroup>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>.\bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Include="UnitTesting\*.cs" />
<Reference Include="NUnit.dll" />
</ItemGroup>
</When>
<When Condition=" '$(Configuration)'=='retail' ">
<PropertyGroup>
<DebugSymbols>false</DebugSymbols>
<Optimize>true</Optimize>
<OutputPath>.\bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<DebugSymbols>true</DebugSymbols>
<Optimize>false</Optimize>
<OutputPath>.\bin\$(Configuration)\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
</Otherwise>
</Choose>
如何在 MsBuild 中創建帶有任何 When Condition 和其他塊的 xml 塊上方的 ChooseElement。
我寫了這樣的代碼但不起作用:
var root = ProjectRootElement.Create();
var choose = root.CreateChooseElement();
var when1 = root.CreateWhenElement("Condition1"); // How add tag for this one ?
var when2 = root.CreateWhenElement("Condition2");
var when3 = root.CreateWhenElement("Condition3");
var when4 = root.CreateWhenElement("Condition4");
var ow = root.CreateOtherwiseElement(); // How add tag for this one ?
choose.AppendChild(when1); // Exception Here !
choose.AppendChild(when2);
choose.AppendChild(when3);
choose.AppendChild(when4);
choose.AppendChild(ow);
root.Save("test.csproj");
例外 :
An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.Build.dll
Additional information: The parent node is not itself parented.
我覺得這個 xml 塊太復雜了!
請任何人指導我。
我知道有點晚。 剛才看到了這個問題。 抱歉耽擱了。
您的代碼存在問題,即您正在使用var choose = root.CreateChooseElement();
創建ProjectElement var choose = root.CreateChooseElement();
但是在將任何內容附加到此元素之前,您應該將元素本身附加到Root元素,即var root = ProjectRootElement.Create();
你缺少 root.AppendChild(choose);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.