我有用csharp
编写的内联任务代码我想知道无论如何都要在内联任务代码中访问propect属性
对于前。我试图用项目属性值替换字符串匹配。有可能吗?
<![CDATA[
MatchCollection matches = Regex.Matches(SourceStr, Pattern);
for (int i = 0; i < matches.Count; i++)
// replace the match value with project property... possible?
]]>
答案 0 :(得分:3)
将它作为参数传递,就像编译任务一样?
<ParameterGroup>
<Foo ParameterType="System.Bar" Required="true" />
<ParameterGroup>
编辑:看起来简单的内联令牌也可以。
<![CDATA[
Console.WriteLine("$(Foo)");
]]>
答案 1 :(得分:2)
作为评论回复太大了。
它是空的原因与您的其他问题一样 - 评估顺序。在命中目标之前对代码进行评估和标记化,我假设您在此处设置值并设置属性。
如果我理解了您要完成的任务,请看下面的示例,只需将它们作为属性传递出去,然后输出为输出。
那就是说,我认为你应该首先考虑属性函数,为自己省去一堆麻烦。
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>
答案 2 :(得分:0)
MSDN documentation中有一些例子。例如:
<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' ToolsVersion="4.0">
<UsingTask TaskName="TokenReplace" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Path ParameterType="System.String" Required="true" />
<Token ParameterType="System.String" Required="true" />
<Replacement ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
<![CDATA[
string content = File.ReadAllText(Path);
content = content.Replace(Token, Replacement);
File.WriteAllText(Path, content);
]]>
</Code>
</Task>
</UsingTask>
<Target Name='Demo' >
<TokenReplace Path="C:\Project\Target.config" Token="$MyToken$" Replacement="MyValue"/>
</Target>
</Project>
如果$(MyValue)
是项目属性,您可以执行以下操作:
<TokenReplace Path="C:\Project\Target.config" Token="$MyToken$" Replacement="$(MyValue)"/>