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

后端 未结 4 644
予麋鹿
予麋鹿 2020-12-05 14:20

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 c

相关标签:
4条回答
  • 2020-12-05 14:36

    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.

    0 讨论(0)
  • 2020-12-05 14:54

    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
    
    0 讨论(0)
  • 2020-12-05 14:57

    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>
    
    0 讨论(0)
  • 2020-12-05 14:58

    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.

    0 讨论(0)
提交回复
热议问题