Thursday, March 27, 2008

Different References between Debug and Release Builds in Visual Studio 2005

What if you want to reference different DLLs in Debug and Release builds of your favorite .NET project? Perhaps you've got some unmanaged DLL spat out by a system that you don't control, but that produces different builds? Or, as in my case, you've got an F# assembly you want to reference from a C# assembly, but F# doesn't yet support "project references", so you have to link directly to the DLL. I'm not convinced what I did will always work, but it seems to right now.

The references in your project (in this case a C# project) are held in a tag called <ItemGroup>. It turns out you can add restrictions to that tag just like you can the other tags in your project, e.g. giving your assembly different names in debug and release modes. So you can end up with something like this.

<ItemGroup> <Reference Include="System" /> <Reference Include="System.configuration" /> </ItemGroup> <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <Reference Include="my favorite debug dll"> <SpecificVersion>False</SpecificVersion> <HintPath>..\..\Debug\so-and-so.dll</HintPath> </Reference> </ItemGroup> <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <Reference Include="my favorite debug dll"> <SpecificVersion>False</SpecificVersion> <HintPath>..\..\Release\so-and-so.dll</HintPath> </Reference> </ItemGroup>