如何从msbuild调用静态类方法?
如何从msbuild调用类静态方法并将其结果存储在列表中 编辑:好的,让我进一步解释一下。我正在使用sandcastle帮助文件生成器为我的应用程序生成文档。其中一项要求是,您必须指定以下文档来源:如何从msbuild调用静态类方法?,msbuild,Msbuild,如何从msbuild调用类静态方法并将其结果存储在列表中 编辑:好的,让我进一步解释一下。我正在使用sandcastle帮助文件生成器为我的应用程序生成文档。其中一项要求是,您必须指定以下文档来源: <DocumentationSources> <DocumentationSource sourceFile="$(MSBuildProjectDirectory)\..\src\myApp\bin\Debug\myApp.exe" xmlns="" /> &
<DocumentationSources>
<DocumentationSource sourceFile="$(MSBuildProjectDirectory)\..\src\myApp\bin\Debug\myApp.exe" xmlns="" />
<DocumentationSource sourceFile="$(MSBuildProjectDirectory)\..\src\myApp\bin\Debug\myApp.xml" xmlns="" />
</DocumentationSources>
Sandcastle帮助文件生成器附带一个utils程序集,该程序集可以从指定目录检索所有dll和xml文件。我想从此程序集中调用该方法,并将其结果存储为
列表。这是一个静态方法,它返回集合创建一个调用该静态方法的自定义任务,并返回一个ITaskItem数组
或
您可以尝试使用程序集。调用:
<PropertyGroup>
<StaticMethodAssemblyPath>path</StaticMethodAssemblyPath>
</PropertyGroup>
<MSBuild.ExtensionPack.Framework.Assembly TaskAction="Invoke"
NetArguments="@(ArgsM)"
NetClass="StaticMethodClassName"
NetMethod="StaticMethodName"
NetAssembly="${StaticMethodAssemblyPath}">
<Output TaskParameter="Result" PropertyName="R"/>
</MSBuild.ExtensionPack.Framework.Assembly>
路径
通常,最灵活的选择是创建一个。这是所有未经测试的代码,旨在让您了解:
在msbuild文件中:
<UsingTask TaskName="FindFiles" AssemblyFile="FindFiles.dll" />
<!--
As you'll see below, SearchDirectory and SearchPatterns are input parameters,
MatchingFiles is an output parameter, SourceFiles is an ItemGroup assigned to
the output.
-->
<FindFiles SearchDirectory="$(MyDirectory)" SearchPatterns="*.dll;*.xml">
<Output ItemName="SourceFiles" TaskParameter="MatchingFiles" />
</FindFiles>
<!-- You can then use the generated ItemGroup output elsewhere. -->
<DocumentationSources>
<DocumentationSource sourceFile="@(SourceFiles)" xmlns="" />
</DocumentationSources>
FindFiles.cs:
using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace FindFiles
{
public class FindFiles : Task
{
// input parameter
[Required]
public string SearchDirectory { get; set; }
// output parameter
[Required]
public string[] SearchPatterns { get; set; }
[Output]
public string[] MatchingFiles { get; private set; }
private bool ValidateParameters()
{
if (String.IsNullOrEmpty(SearchDirectory))
{
return false;
}
if (!Directory.Exists(SearchDirectory))
{
return false;
}
if (SearchPatterns == null || SearchPatterns.Length == 0)
{
return false;
}
return true;
}
// MSBuild tasks use the command pattern, this is where the magic happens,
// refactor as needed
public override bool Execute()
{
if (!ValidateParameters())
{
return false;
}
List<string> matchingFiles = new List<string>();
try
{
foreach (string searchPattern in SearchPatterns)
{
matchingFiles.AddRange(
Directory.GetFiles(SearchDirectory, searchPattern)
);
}
}
catch (IOException)
{
// it might be smarter to just let this exception fly, depending on
// how you want the task to behave
return false;
}
MatchingFiles = matchingFiles.ToArray();
return true;
}
}
}
使用系统;
使用System.IO;
使用System.Collections.Generic;
使用Microsoft.Build.Framework;
使用Microsoft.Build.Utilities;
命名空间FindFiles
{
公共类FindFile:任务
{
//输入参数
[必需]
公共字符串搜索目录{get;set;}
//输出参数
[必需]
公共字符串[]搜索模式{get;set;}
[输出]
公共字符串[]匹配文件{get;private set;}
私有bool ValidateParameters()
{
if(String.IsNullOrEmpty(SearchDirectory))
{
返回false;
}
如果(!Directory.Exists(SearchDirectory))
{
返回false;
}
if(SearchPatterns==null | | SearchPatterns.Length==0)
{
返回false;
}
返回true;
}
//MSBuild任务使用命令模式,这就是神奇之处,
//根据需要重构
公共重写bool Execute()
{
如果(!ValidateParameters())
{
返回false;
}
列表匹配文件=新列表();
尝试
{
foreach(SearchPatterns中的字符串searchPattern)
{
matchingFiles.AddRange(
Directory.GetFiles(SearchDirectory,searchPattern)
);
}
}
捕获(IOException)
{
//根据具体情况,让这个异常继续运行可能更明智
//您希望任务表现如何
返回false;
}
MatchingFiles=MatchingFiles.ToArray();
返回true;
}
}
}
自定义任务很好,但如果您想做一些简单的事情,则可能会有过多的工作量。我相信德拉科是在问这个问题
使用静态函数设置属性的示例(直接从上页撕下):
或者,也许你想要更疯狂的东西。我不明白你的问题。msbuild是用于生成(编译/链接)的实用程序,而不是编程语言。你到底想干什么?很抱歉。我已经更新了questionInline任务,它们就像MSBuild脚本中的LINQPad脚本!谢谢你的启发!有关可用类型的列表,请参见。
<Today>$([System.DateTime]::Now)</Today>
$([Class]:: Property.Method(Parameters))