2016-04-23 4 views
0

Ich möchte programmatisch für die Erstellung von CSPROJ Datei etwas wie unten zu schaffen, ichIch möchte ChooseElement für .csproj mit MSBuild erstellen?

  • Namespace verwenden: Microsoft.Build.Construction
  • Montage: Microsoft.Build (in Microsoft.Build.dll)

zum Beispiel:

<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> 

Wie kann ich ChooseElement in MsBuild erstellen, die mit jedem Wenn Bedingung über xML-Block erstellen und Sonst blockieren.

Ich schrieb einen Code wie diesen aber nicht funktioniert:

 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"); 

Ausnahme:

An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.Build.dll 

Additional information: The parent node is not itself parented. 

Ich denke, das XML-Block so komplex ist!

Bitte jemand führen mich.

Antwort

0

Ich weiß sein bisschen spät. Gerade jetzt diese Frage gesehen. Entschuldigung für die Verspätung.

Problem mit Ihrem Code ist Sie erstellen ein Project var choose = root.CreateChooseElement(); verwenden, aber bevor irgendetwas in diesem Element anhängen sollten Sie Element selbst mit dem Wurzelelement dh anhängen., var root = ProjectRootElement.Create();

Verwandte Themen