slimCODE

commUNITY
Welcome to slimCODE Sign in | Join | Help
in Search

slimCODE, aka Martin Plante

Referencing a debug assembly in debug builds, and a release assembly in release builds

I have a few VS.NET 2008 projects that share usage of libraries I made for Windows Mobile. I used to include the libraries’ projects directly into each solution, but this is leading me to rebuilding those libraries each time, even if the sources do not change often. It also could cause two applications to look like they’re using the same build of those libraries, while they’re not actually the same bits.

So I decided to move those libraries outside my solutions, and have a separate build for each. Now, I still wanted to reference the debug builds when building the debug versions of my applications, and the release builds when building in release. Turns out you can hack into your project files to accomplish exactly that.

Open your .csproj files with a text editor and locate your references. They should look like this:

<Reference Include="slimCODE.slimCONTROLS, Version=1.0.9312.0, Culture=neutral, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\..\Binaries\Release\slimCODE.slimCONTROLS.dll</HintPath>
</Reference>

There are two ways to make the above reference change target based on the active configuration.

Add a condition

You can add a “Condition” attribute to the <Reference> tag that will make that reference toggle based on that condition. It can be anything. In our case, it would look like this (important stuff in red):

<Reference Include="slimCODE.slimCONTROLS, Version=1.0.9312.0, Culture=neutral, processorArchitecture=MSIL" Condition="'$(Configuration)' == 'Release'">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\..\Binaries\Release\slimCODE.slimCONTROLS.dll</HintPath>
</Reference>
<Reference Include="slimCODE.slimCONTROLS, Version=1.0.9312.0, Culture=neutral, processorArchitecture=MSIL" Condition="'$(Configuration)' == 'Debug'">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\..\Binaries\Debug\slimCODE.slimCONTROLS.dll</HintPath>
</Reference>

Thus, for each reference you want conditional, you must add a second entry for each subsequent configuration name. Don’t forget to change the <HintPath> accordingly.

Cheat the hint

Turns out the <HintPath> can contain variables, like $(Configuration). That’s an easier way to redirect to the proper build, given your binaries are located in folders using the configuration names. That was my case.

<Reference Include="slimCODE.slimFORMS, Version=1.0.9312.0, Culture=neutral, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\..\Binaries\$(Configuration)\slimCODE.slimFORMS.dll</HintPath>
</Reference>

Though the first way looks more Kosher, the second way seems to behave correctly too, is simpler, and adapts automatically for each new configuration, given you use the correct folder names. Changing the active configuration does make the reference’s “Path” value in the “Properties” window change accordingly.

I’ll try both and give you more feedback if one fails.

Published Friday, June 12, 2009 6:29 AM by slimcode
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit