编写自定义任务时如何访问MSBuild的属性列表? [英] How to access the MSBuild 's properties list when coding a custom task?
本文介绍了编写自定义任务时如何访问MSBuild的属性列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要编写一个自定义任务,以打印所有定义的属性(非保留属性).因此,在我的C#代码中,我想访问MSBuild引擎的属性列表,但我不知道该怎么做.请帮忙.
I need to write a custom task that print all the defined properties (the non-reserved ones). So in my C# code, I wanna access to the properties list of MSBuild engine and I don't know how. Please help.
推荐答案
前面的示例将锁定您的项目文件.这可能会引起问题.例如,如果您在同一项目文件中多次调用任务.这是改进的代码:
The previous example will lock you project file. This may cause problems. For example if you call the task several times in the same project file. Here is improved code:
using System.Xml;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Utilities;
namespace MSBuildTasks
{
public class GetAllProperties : Task
{
public override bool Execute()
{
using(XmlReader projectFileReader = XmlReader.Create(BuildEngine.ProjectFileOfTaskNode))
{
Project project = new Project(projectFileReader);
foreach(ProjectProperty property in project.AllEvaluatedProperties)
{
if(property.IsEnvironmentProperty) continue;
if(property.IsGlobalProperty) continue;
if(property.IsReservedProperty) continue;
string propertyName = property.Name;
string propertyValue = property.EvaluatedValue;
// Do your stuff
}
return true;
}
}
}
}
这篇关于编写自定义任务时如何访问MSBuild的属性列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文