Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Project: How to include a reference for one configuration only?

Env.: VS2008 C# project

I need to build my app for use in 2 different environments. In one of those environments, I need to use a 3rd party DLL assembly.

I could isolate the code that uses this DLL using #if blocks. But how do I conditionally include the reference to the DLL in the CS project file?

Edit: womp has a good point in his comment. I turned into a separate question: Will the referenced DLL be loaded at all if it's never called? TIA,

like image 920
Serge Wautier Avatar asked Sep 29 '09 16:09

Serge Wautier


People also ask

How do I add a reference to Visual Studio project?

To add a reference, right click on the References or Dependencies node in Solution Explorer and choose Add Reference. You can also right-click on the project node and select Add > Reference.

What is the difference between Dependencies and references in Visual Studio?

They are basically no different, they are used to store and manage references.


3 Answers

Unload the project and open it as .XML

Locate the reference item tag and add a Condition attribute.

For instance:

<ItemGroup>
  <Reference Include="System.Core">
    <RequiredTargetFramework>3.5</RequiredTargetFramework>
  </Reference>
  <Reference Include="System.Data" />
  <Reference Include="System.Drawing" />
  <Reference Include="System.Xml" />

  <Reference Include="MyUtilities.Debug"
    Condition="'$(Configuration)'=='Debug'"/>

</ItemGroup>

Notice the last reference now has a condition.

like image 60
Coincoin Avatar answered Oct 18 '22 22:10

Coincoin


I know this is an old post, but in case anyone else finds it before they find the answer, like I did, it's this: you need to use the "Choose" element in the project file:

link

You can define both conditional references and conditional compilation in one place, so you don't have to use #if's in your code.

It works in SharpDevelop, and since it's MS's documentation I assume it works in Visual Studio.

like image 34
Andrew Avatar answered Oct 18 '22 22:10

Andrew


The following, in the csproj file references itemgroup works in vs 2008 for me:-

<Reference Include="DRLClasses, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL" Condition=" '$(Configuration)' == 'Debug' ">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\..\..\..\Visual Studio User Library\Debug\DRLClasses.dll</HintPath>
</Reference>
<Reference Include="DRLClasses, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL" Condition=" '$(Configuration)' == 'Release' ">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\..\..\..\Visual Studio User Library\Release\DRLClasses.dll</HintPath>
</Reference>
like image 24
cooldrl Avatar answered Oct 18 '22 23:10

cooldrl