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

Viewed 20750

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,

4 Answers

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
Related