14

Visual Studio 2013.

I have an external DLL which I am referencing like this in the csproj file:

  <ItemGroup>
    <Reference Include="NameOfDll">
      <HintPath>Path\To\Dll\NameOfDll.dll</HintPath>
    </Reference>

I want this reference to function when a compiler symbol exists and to not function when that compiler symbol does not exist. (To address the first comment, below, let's say the compiler symbol is called Fred.)

This question [ Conditional Reference ] made me think I could add an attribute called Condition to the Reference element shown above but I can't work out what value to give that attribute to effect what I want.

I'd be most happy to be given a way to do this in the VS UI but I'll take any method.

Community
  • 1
  • 1
cja
  • 9,512
  • 21
  • 75
  • 129
  • @HansPassant Please assume the compiler symbol is called Fred if that helps you to understand the question. Is a compiler symbol a build property? – cja Feb 07 '15 at 21:35
  • The conditional compiler symbols are in the DefineConstants build property. you'd check that DefineConstants contained some string in the condition. Something like `Condition="$(DefineConstants.Contains('Fred'))"` – Mike Zboray Feb 07 '15 at 21:36
  • @mikez Please give a little more detail in an answer and I'll mark it correct – cja Feb 07 '15 at 21:38
  • 3
    Please could the close voter explain themselves – cja Feb 08 '15 at 15:28

1 Answers1

26

The conditional compilation symbols are in the DefineConstants MSBuild property. Check that this contains your symbol:

<Reference Include="NameOfDll" Condition="$(DefineConstants.Contains('Fred'))">
  <HintPath>Path\To\Dll\NameOfDll.dll</HintPath>
</Reference>

Pick a distinctive name for the symbol. Not something that could be a substring of another constant like Debug or Trace.

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122