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 ]