在MSBuild中创建一个字符串的ItemGroup

Jus*_*tin 0 msbuild itemgroup

我想创建一个任意字符串/名称的"ItemGroup",以便使用MSBuild转换,例如:

<ItemGroup>
    <Categories>First</Categories>
    <Categories>Second</Categories>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

然后,我希望将这些类别的转换传递到控制台应用程序,例如:

/c @(Categories, ' /c ')
Run Code Online (Sandbox Code Playgroud)

我在引号中说"ItemGroup"的原因是因为我不确定它是否适用于我以这种方式使用ItemGroups - 据我所知,文档中没有任何内容表明ItemGroups 必须是文件,但由于缺少必需的"包含"属性,因此在错误消息中使用上述结果.

  • 有没有办法使用ItemGroups执行上述操作?
  • 或者,有没有更好的方法来实现上述不使用ItemGroups?

Jul*_*rau 6

您可以使用任意字符串以及文件Item,但必须使用以下语法:

<ItemGroup>
  <Categories Include="First"/>
  <Categories Include="Second"/>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

使用Item任意字符串时唯一的区别是某些元数据将毫无意义.(%(Categories.FullPath)例如)

然后,您可以使用您的Item执行如下命令:

<Target Name="ExecCommand">
  <Exec Command="YourProgram.exe /c @(Categories, ' /c ')"/>

  <!-- Using transformation -->
  <Exec Command="YourProgram.exe @(Categories -> '/c %(Identity)')"/>
</Target>
Run Code Online (Sandbox Code Playgroud)

  • +1,但注意 - @(类别,'/ c')表示"/ c"将是项目之间的*分隔符*,因此您的最后一项将缺少/ c(或根据您的POV的第一项).你真的需要像这样的*变换*:@(Categories - >'/ c%(Identity)')来获取你所有物品前面的/ c (2认同)