Conditional Library References in Visual Studio


I recently ran into a situation where I needed to have a single project in Visual Studio build the application two different ways with each way using different DLL references. The normal Project Properties provide all sorts of mechanisms for build-specific options but Microsoft seems to have left the ability to specify conditional library references out of it for some reason.

It turns out that while they did leave it out of Visual Studio, it is available in MSBuild which Visual Studio uses to actually compile the code.

Here is how it works:

  1. Open your solution in Visual Studio
  2. Select Build – Configuration Manager and select <New…> from the drop down in the top left corner.
  3. Create the new config based on whatever current profile makes senseVisual Studio Configuration Manager
  4. Hit OK to create your alternative configuration
  5. Right-click on your project file in Visual Studio and select ‘Open folder in Windows Explorer’
  6. Close Visual Studio
  7. Use your favorite text editor to edit the Visual Studio Project file (*.csproj or *.vbproj)
  8. Locate the library reference that you want to make conditional within the <ItemGroup> element
  9. Duplicate the entire <Reference… > nodes that you want to vary by build
  10. For each reference, add a new ‘Condition=…’ attribute as shown in the block below
    <ItemGroup>
    <Reference Include="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL" Condition="'$(Configuration)'=='Debug2007'">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\DLLReferences\2007\Microsoft.SharePoint.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL" Condition="'$(Configuration)'=='Debug2010'">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\DLLReferences\2010\Microsoft.SharePoint.dll</HintPath>
      <Private>True</Private>
    </Reference>
    </ItemGroup>
    
  11. The basic syntax looks clumsy but is actually pretty straightforward: Condition=”‘$(Configuration)’==’YourBuildName1′”. You will need to specify this for each of your builds
  12. Save the file and reopen the solution in Visual Studio. You should now be able to simply change your build configuration from the toolbar and build using each set of DLLs

The Visual Studio UI doesn’t always keep up with the conditional references and you may see what appears to be the ‘broken reference’ icon next to your references in the tree. This is nothing to worry about though as everything will still build fine and the UI does catch up eventually.

Also, you may want to change the Output Path (Project Properties – Build) to different folders for each of the build configurations in order to prevent them from stepping on each other.

Naturally, once I had a working solution and knew exactly what to look for I was able to track down the actual documentation from Microsoft, kinda. This approach also supports Platform-specific conditions as well.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s