编写自定义任务时如何访问 MSBuild 的属性列表? [英] How to access the MSBuild 's properties list when coding a custom task?

查看:19
本文介绍了编写自定义任务时如何访问 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆