July, 2013

  • Kirill Osenkov

    Dump MSBuild properties and items for a project

    • 2 Comments

    Sometimes when you’re reading a .csproj file you encounter properties like $(TargetDir) and wonder what directory does this property evaluate to when the project is compiled. Of course you can use heavy machinery like the MSBuild debugger, but sometimes a simpler tool can suffice. Here’s what I wrote in literally 5 minutes:

    using System;
    using System.IO;
    using Microsoft.Build.Evaluation;
     
    class MSBuildDumper
    {
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Usage: MSBuildDumper <path-to-project.csproj>");
                return;
            }
     
            var projectFilePath = Path.GetFullPath(args[0]);
            var project = new Project(projectFilePath); // add reference to Microsoft.Build.dll to compile
     
            foreach (var property in project.AllEvaluatedProperties)
            {
                Console.WriteLine(" {0}={1}", property.Name, property.EvaluatedValue);
            }
     
            foreach (var item in project.AllEvaluatedItems)
            {
                Console.WriteLine(" {0}: {1}", item.ItemType, item.EvaluatedInclude);
            }
        }
    }

    The API I’m using is called MSBuild evaluation API, it reads MSBuild project files and makes sense of their content. Try running this tool on one of your .csproj project files and be amazed at how much stuff is happening during build evaluation.

Page 1 of 1 (1 items)