具有条件的MSBuild ItemGroup [英] MSBuild ItemGroup with condition
问题描述
我不知道ItemGroup
是否是正确的类型.根据选择,我将得到4个不同的布尔值,它们是对还是错.
I don't know if ItemGroup
is the right type to use. I will get 4 different booleans that will be true or false depending on choice.
我想用字符串"填充ItemGroup
,具体取决于对还是错.那有可能还是我应该使用什么?
I would like to fill up an ItemGroup
with this "strings" depending on the true or false. Is that possible or what should I use?
示例
Anders = true
Peter = false
Michael = false
Gustaf = true
然后我的ItemGroup
应该有
安德斯和古斯塔夫.
My ItemGroup
should then have
Anders and Gustaf.
有可能还是应该解决呢?
Is that possible or how should I solve that?
推荐答案
由于您有一堆物品,因此从一开始就将它们存储在ItemGroup
中会更好,因为毕竟这是要的并且还允许进行转换等.例如,这可以实现您想要的:
Since you have a bunch of items, it would be better to store them in an ItemGroup
from the start since after all that is what it is meant for and it also allows transformations etc. For example this achieves what you want:
<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>
但是如果它们必须是属性,我认为除了手动枚举它们之外,别无其他方法:
However if they must be properties, I do not think there is another way than enumerating them all manually like so:
<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>
这篇关于具有条件的MSBuild ItemGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!