visual-studio Visual Studio 项目:如何仅包含一个配置的参考?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1493749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:03:12  来源:igfitidea点击:

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

c#visual-studioconfigurationmsbuildreference

提问by Serge Wautier

Env.: VS2008 C# project

环境:VS2008 C#项目

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.

我需要构建我的应用程序以在 2 个不同的环境中使用。在其中一种环境中,我需要使用 3rd 方 DLL 程序集。

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?

我可以使用 #if 块隔离使用此 DLL 的代码。但是我如何有条件地在 CS 项目文件中包含对 DLL 的引用?

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,

编辑:womp 在他的评论中有一个很好的观点。我变成了一个单独的问题:如果引用的 DLL 从未被调用过,它会被加载吗?TIA,

回答by Coincoin

Unload the project and open it as .XML

卸载项目并将其作为 .XML 打开

Locate the reference item tag and add a Condition attribute.

找到引用项标记并添加 Condition 属性。

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.

请注意,最后一个引用现在具有条件。

回答by Andrew

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.

您可以在一处定义条件引用和条件编译,因此您不必在代码中使用 #if。

It works in SharpDevelop, and since it's MS's documentation I assume it works in Visual Studio.

它适用于 SharpDevelop,而且由于它是 MS 的文档,我假设它适用于 Visual Studio。

回答by cooldrl

The following, in the csproj file references itemgroup works in vs 2008 for me:-

以下,在 csproj 文件中引用 itemgroup 对我来说适用于 vs 2008:-

<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>

回答by Dan W

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>命令。例如:

<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 /pproperty in MSBuild like this:

然后在 CLI 中,只需/p像这样使用MSBuild 中的属性:

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

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

...或者如果您不想要 DLL,只需:

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