MSBuild带有条件的ItemGroup

9

我不确定是否应该使用ItemGroup类型。根据选择,我将获得4个不同的布尔值,这些值将是true或false。

我想用这些“字符串”填充一个ItemGroup,具体取决于true或false。这可能吗?或者我应该使用什么?

示例

Anders = true
Peter = false
Michael = false
Gustaf = true

我的ItemGroup应该包含安德斯和古斯塔夫。

这个是否可能,或者我应该怎样解决?


这些“strings”是什么?它们在文本文件中吗?它们是MSBuild属性吗?另外,请检查您的拼写。应该是ItemGroup而不是iteamgroup - stijn
1个回答

13

既然你有很多项,最好从一开始就将它们存储在一个中,因为毕竟这就是它的本意,同时它还允许进行转换等操作。例如,以下代码可以实现你想要的功能:

<ItemGroup>
  <Names Include="Anders">
    <Value>True</Value>
  </Names>
  <Names Include="Peter">
    <Value>False</Value>
  </Names>
  <Names Include="Michael">
    <Value>False</Value>
  </Names>
  <Names Include="Gustaf">
    <Value>True</Value>
  </Names>
</ItemGroup>

<Target Name="GetNames">

  <ItemGroup>
    <AllNames Include="%(Names.Identity)" Condition="%(Names.Value)==true"/>
  </ItemGroup>

  <Message Text="@(AllNames)"/>  <!--AllNames contains Anders and Gustaf-->
</Target>

但是如果它们必须成为属性,我认为没有其他方法,只能像这样手动列举它们:

<PropertyGroup>
  <Anders>True</Anders>
  <Peter>False</Peter>
  <Michael>False</Michael>
  <Gustaf>True</Gustaf>
</PropertyGroup>

<Target Name="GetNames">

  <ItemGroup>
    <AllNames Include="Anders" Condition="$(Anders)==true"/>
    <AllNames Include="Peter" Condition="$(Peter)==true"/>
    <AllNames Include="Michael" Condition="$(Michael)==true"/>
    <AllNames Include="Gustaf" Condition="$(Gustaf)==true"/>
  </ItemGroup>

  <Message Text="@(AllNames)"/>
</Target>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接