太大,无法作为评论回复。
它为空的原因与您的另一个问题一样 - 评估顺序。在达到目标之前对代码进行评估和标记化,我假设这是您设置值和设置属性的地方。
如果我了解您要通过该任务实现的目标,请看下面的示例,只需将它们作为属性传递并作为输出传递。
也就是说,我认为你应该先研究一下属性函数,为自己省去很多麻烦。
http://msdn.microsoft.com/en-us/library/dd633440.aspx
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="Foo" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<SourceStr Required="true" Output="true" />
<Pattern Required="true" />
<Value Required="true" />
<Macros ParameterType="Microsoft.Build.Framework.ITaskItem[]" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.Linq" />
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
var regex = new Regex(Pattern);
var matches = regex.Matches(SourceStr).Cast<Match>().Select(m => m.Value).ToList();
matches.ForEach(m => Log.LogMessage("{0} -> {1}", m, Value));
Macros = matches.Select(m => new TaskItem(m)).ToArray();
SourceStr = regex.Replace(SourceStr, Value);
]]>
</Code>
</Task>
</UsingTask>
<Target Name="Foo">
<PropertyGroup>
<SourceStr>Bar</SourceStr>
<Value>z</Value>
</PropertyGroup>
<Message Text="in $(SourceStr)" />
<Foo SourceStr="$(SourceStr)" Pattern="r$" Value="$(Value)">
<Output TaskParameter="SourceStr" PropertyName="SourceStr" />
<Output TaskParameter="Macros" ItemName="Macros" />
</Foo>
<Message Text="out $(SourceStr)" />
<Message Text="sans %(Macros.Identity)" />
</Target>
</Project>