22

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,

Community
  • 1
  • 1
Serge Wautier
  • 21,494
  • 13
  • 69
  • 110
  • 2
    Why wouldn't you just leave it in both, make the usage of the dll configuration based, and avoid complicating things? DLLs aren't loaded unless necessary. Are there licensing issues with the 3rd party dll? – womp Sep 29 '09 at 16:59
  • womp, very good question actually. (I don't distribute the 3rd party DLL. It's installed on some systems by the manufacturer). – Serge Wautier Oct 01 '09 at 12:37

4 Answers4

25

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.

geocodezip
  • 158,664
  • 13
  • 220
  • 245
Coincoin
  • 27,880
  • 7
  • 55
  • 76
  • 2
    That was my thought too, so I tried it. In my case that caused the reference to fail in all configuration (the system acted as if it could not find the assembly). – Fredrik Mörk Sep 29 '09 at 17:25
  • 2
    Yeah the problem is, the IDE ignores things with conditions and in that case, it needs it for all kind of reason (intellisense, object browser...) so it will complain. Also, you will have to make the calls to that assembly conditional, else the compiler won't be able to find the assembly the code is refering to. – Coincoin Sep 29 '09 at 17:32
  • Just to help others maybe: When using MSBuild in the CLI, use the /p flag (e.g: `/p:MYFLAG=true`) to set the variable within the XML. – Dan W Nov 30 '18 at 05:00
2

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.

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
Andrew
  • 21
  • 1
1

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>
cooldrl
  • 11
  • 1
  • 5
    You can simplify this by just adding the `$(Configuration)` variable directly in the HintPath: ` False ..\..\..\..\Visual Studio User Library\$(Configuration)\DRLClasses.dll ` – awe Feb 28 '11 at 11:32
0

Inspired by the question and answer shown here, you can add <Choose> and <When Condition> commands around the part you want to be conditionally run. For example:

<Choose>
  <When Condition="$(USEDLL) == true">

    <ItemGroup>
    <EmbeddedResource Include="test.dll">
    <LogicalName>test.dll</LogicalName>
    </EmbeddedResource>
    </ItemGroup>

  </When>
</Choose>

Then in the CLI, simply use the /p property in MSBuild like this:

MSBuild "C:\myproject\myproject.sln" /p:USEDLL=true

...or if you don't want the DLL, simply:

MSBuild "C:\myproject\myproject.sln" /p:USEDLL=false
Dan W
  • 3,520
  • 7
  • 42
  • 69