繁体   English   中英

我想用 MSBuild 为 .csproj 创建 ChooseElement 如何?

[英]I want to create ChooseElement for .csproj with MSBuild How?

我想创建类似下面的东西来以编程方式创建 .csproj 文件,我使用

  • 命名空间:Microsoft.Build.Construction
  • 程序集:Microsoft.Build(在 Microsoft.Build.dll 中)

例如 :

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM