Share via


MSBuild in Visual Studio Part 2: How the IDE Reads Properties

Many of the properties in an MSBuild project file show up directly within the Visual Studio environment. The most common place you’ll see the properties show up is in the project properties screen:

How do these properties get from the MSBuild XML in the project file into the dialog? Well, as you might guess, the project system uses methods on the MSBuild object model. You might think it’s as straightforward as just asking for the property from the file, but notice those two pesky drop-downs in the screenshot for configuration and platform. Somehow the project system needs to get different values back from the project file depending on what the user has picked for configuration and platform.

If you crack open a project file you can see how all these settings are stored. Here’s a snippet from a VB project file with the settings for the Debug|Any CPU configuration:

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">

    <DebugSymbols>true</DebugSymbols>

    <DebugType>full</DebugType>

    <DefineDebug>true</DefineDebug>

    <DefineTrace>true</DefineTrace>

    <OutputPath>bin\Debug\</OutputPath>

    <DocumentationFile>WindowsApplication13.xml</DocumentationFile>

    <NoWarn>42016,41999,42017,42018,42019,42032</NoWarn>

  </PropertyGroup>

Notice the conditional on the PropertyGroup element. To get back the values for that section, the project system first sets the Configuration and Platform properties to Debug and Any CPU, then retrieves the rest of the properties. The code looks something like this:

Project.GlobalProperties.SetProperty(“Configuration”, “Debug”);
Project.GlobalProperties.SetProperty(“Platform”, “Any CPU”);
string outputPath = Project.EvaluatedProperties[“OutputPath”].FinalValue;

MSBuild will automatically work out all the conditionals and return the correct values. Pretty neat!

[ Author: Neil Enns ]

Comments

  • Anonymous
    January 18, 2007
    More than year passed before first comment :) Could you describe how to read and write array of properties (list of addittional libraries for example) and send them to msbuild task after? I couldn't find in API anything better than passing simple string :( I mean those things, that come to msbuild task as @(AdditionalLibs) ?

  • Anonymous
    August 17, 2008
    PingBack from http://www.developerzen.com/2006/01/07/msbuild-in-visual-studio/

  • Anonymous
    November 22, 2009
    Is there a way to write custome project properties that would appear on the same GUI as the native project properties?