views:

608

answers:

2

Env.: VS2008 C# project

Hi,

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,

Serge.

+3  A: 

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.

Coincoin
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
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
A: 

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