36

When adding a user control or a project reference to a VS 2008 C# project, I can add only one configuration of the assembly. Is it possible to add separate configurations, depending on the configuration of the container project.

E.g. I am developing a user control, and I am working on a sample application. I want to add the user control so that a debug build of the sample will use the debug build of the user control, and the release build of the sample the release build of the user control.

Any suggestions?

froh42
  • 5,190
  • 6
  • 30
  • 42
peterchen
  • 40,917
  • 20
  • 104
  • 186

4 Answers4

33
<Reference Include="MyLibrary">
  <HintPath>..\$(Configuration)\MyLibrary.dll</HintPath>
</Reference>

This add a reference "..\Debug\MyLibrary.dll" if compiled in debug mode or ..\Release\MyLibrary.dll" if compiled in release mode.

Salvatore Previti
  • 8,956
  • 31
  • 37
  • Great answer + uses relative path as the dll might be in the folder just outside from the solution. – CallMeLaNN Mar 15 '11 at 06:19
  • And Visual Studio (VS2010 in my case) seems to be polite and it does not disturb such hand-edits when one later modifies the project-file from the IDE. As a test I added a new class, everything worked fine. – Radim Cernej Apr 07 '15 at 23:07
29

You can do this by editing the csproj file; add a "Condition" attribute to the reference.

<Reference Include="Foo" Condition="'$(Configuration)'=='Debug'"/>
<Reference Include="Bar" Condition="'$(Configuration)'=='Release'"/>

However, I would have concerns about what this means for unit testing.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    Question is about to use the same `UserControl`, by this way we can't add reference to twice and Visual Studio not allowing it @Salvatore Previti answer the best one. – CallMeLaNN Mar 15 '11 at 06:16
  • This is not working for me (Visual Studio 2022), I solved with @Salvatore Previti answer too – MAXE Mar 23 '23 at 14:15
5

While @Marc Gravell's suggestion will work, is there a reason that you don't want both projects in the same solution? If they are in the same solution, you can add a Project Reference when referencing the User Control project to the sample app's project. When a Project Reference is used, Visual Studio will automatically add the Debug version for a Debug build, and the Release version for the Release build.

Andy
  • 30,088
  • 6
  • 78
  • 89
  • 1
    If the referenced project is under source control separately (maybe it's a general purpose library project) then issues may occur when attempting to add the referencing project to source control. – Alan B Jul 16 '14 at 09:50
1

Instead of adding reference to a .dll directly, which forces you to choose between the .dll from debug or release folder, you should add reference by choosing 'Project reference'. This link explains how to add reference through .dll vs project-project reference. For your purpose, you should choose the latter.

Also refer to my answer to know when to add reference as a .dll vs reference as a project.

theandroid
  • 809
  • 1
  • 6
  • 13