Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the MSBuild 's properties list when coding a custom task?

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.

like image 434
Nam G VU Avatar asked May 05 '10 10:05

Nam G VU


1 Answers

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;
            }
        }
    }
}
like image 87
Roman Terekhov Avatar answered Oct 19 '22 10:10

Roman Terekhov