在MSBuild中构建字符串作为n次基本字符串的连接
我在MSBuild的属性中有一个数字"n".我也有一个字符串"Str",需要重复n次才能获得最终的字符串,该字符串是"Str"的n次重复.
I have a numer, "n" in a property in MSBuild. I also have a string "Str" that needs to be duplicated n-times to achieve a final string that is the repetition of "Str" n times.
例如.如果n为3,且Str为"abc",我想获取的是"abcabcabc"
Eg. If n is 3 and Str is "abc", what I want to obtain is "abcabcabc"
由于无法在MSBuild中循环,所以我不知道该如何实现.也许有一个项目组,但是如何基于包含"n"个计数的属性创建一个项目?
Since one cannot loop in MSBuild, I don't know how to achieve this. Perhaps with an item group, but how do I create one based on a property containing an "n" count?
谢谢!
通常,对于此类问题,我决定使用内联C#,因为与在Internet上查找真正的" msbuild解决方案相比,它花费的时间更少.你去了:
usually for things like this I resolve to using inline C#, as it costs me less time than searching all over the internet to find a 'true' msbuild solution; here you go:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MyString>abc</MyString>
<Count>3</Count>
</PropertyGroup>
<UsingTask TaskName="RepeatString" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<s ParameterType="System.String" Required="true" />
<n ParameterType="System.Int32" Required="true" />
<result ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs"><![CDATA[
result = string.Concat( Enumerable.Repeat( s, n ) );
]]></Code>
</Task>
</UsingTask>
<Target Name="doit">
<RepeatString s="$(MyString)" n="$(Count)">
<Output PropertyName="result" TaskParameter="result" />
</RepeatString>
<Message Text="Result = $(result)"/>
</Target>
</Project>