58

I have a project that adds some extensibility to another application through their API. However, I want to be able to use the same project for multiple versions of their application, because most of the code is the same.

However, each version of the application requires a reference to the proper assembly for that version of the software. They load their assemblies into the GAC, so even if I could specify the version of the assembly to use based on build configuration I would be fine. Is there a way to do this from inside of VS or do I need an external build tool?

snicker
  • 6,080
  • 6
  • 43
  • 49

2 Answers2

63

There is a way to do this, but you will have to hand edit your project files. The project files can have a Condition attribute applied to them in many of the elements, including the one for references.

You can add these to your references to specify when the reference should be used:

<Reference Include="Product, Version=1.0.0.0" Condition="'$(Configuration)'=='V1'">
</Reference>
<Reference Include="Product, Version=2.0.0.0" Condition="'$(Configuration)'=='V2'">
</Reference>
<Reference Include="Product, Version=3.0.0.0" Condition="'$(Configuration)'=='V3'">
</Reference>

You then define several build configurations (V1, V2, V3) and each reference will be included only in the relevant chosen build configuration.

Combine this with conditional compilation symbols and #if statements in your code and you should be able to do what you want.

A thing to be careful of if you do this is that it is easy to have Visual Studio remove the conditional attributes from the project file.

adrianbanks
  • 81,306
  • 22
  • 176
  • 206
  • This worked perfectly, tons of kudos. Even Visual Studio plays along nicely as long as I don't play with those references, including functional Intellisense! – snicker Nov 30 '09 at 21:46
  • 7
    I used a similar approach to make a configuration specific reference to Debug/Release versions of the reference. I just pointed to the release version when I added the reference, and then in Notepad, replaced `Release` in the Hint path with `$(Configuration)`, and voila - the reference updates automatically to the correct target configuration! – awe Feb 28 '11 at 11:26
  • 3
    @awe: It is valid to put the `Condition` attribute on most elements in an MSBuild file. – adrianbanks Feb 28 '11 at 18:49
  • @adrianbanks: Would you happen to know what would trigger Visual Studio to remove the conditional attributes from the project file? I tested adding a new Reference in VS2012, and my hand edits were not affected. I tested using both your and awe's suggested edits, and they remained unchanged by Visual Studio after saving the project. – Walter Stabosz Apr 28 '14 at 14:19
  • 1
    @WalterStabosz Sorry, I don't know, although I would imagine that it shouldn't really remove them ever. – adrianbanks Apr 28 '14 at 14:55
  • You can move such references from the project file into a separate .targets file and then import it. – chase Aug 22 '19 at 07:54
14
<Reference Include="log4net, Version=1.2.11.0, Culture=neutral, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\SharedLibs\log4net\$(Platform)\$(Configuration)\log4net.dll</HintPath>
</Reference>

You can replace the hint path with the properties:

$(Configuration) is equivalent to Release/Debug or whatever other configuration you have. $(Platform) is equivalent to x86/x64/Any CPU

If your configuration includes Any CPU then you will need to put single quotes around $(Configuration)

Also refer to the condition options referenced by adrianbanks

sweetfa
  • 5,457
  • 2
  • 48
  • 62
  • Thanks this worked for me, whereas @adrianbanks' solution would not resolve the packages when trying to build – HostMyBus Mar 09 '18 at 13:22