Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to specify assembly references based on build configuration in Visual Studio?

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?

like image 237
snicker Avatar asked Nov 23 '09 23:11

snicker


People also ask

How do I change the reference path in Visual Studio?

If you're using Visual Basic, select the References page, and then click the Reference Paths button. In the Reference Paths dialog box, type the path of the folder that contains the item you want to reference in the Folder field, and then click the Add Folder button.

How do you use assembly references?

In the Project Designer, click the References tab. Click the Add button to open the Add Reference dialog box. In the Add Reference dialog box, select the tab indicating the type of component you want to reference. Select the components you want to reference, and then click OK.


1 Answers

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.

like image 128
adrianbanks Avatar answered Oct 02 '22 13:10

adrianbanks