Visual Studio 2010 使用第三方库的调试或发布版本进行编译,具体取决于我的项目是编译构建还是发布?

发布于 2024-10-28 08:49:06 字数 152 浏览 6 评论 0原文

我现在已经为 Visual Studio 2010/C# 下载了许多第 3 方库 (dll),并且我注意到在它们的发行版 \bin 目录中,它们通常有两个版本:调试和发布。

有没有办法将这些库添加为项目的引用,但使用发布版本(当我构建版本时),并使用调试版本(当我调试时)?

I've downloaded a number of 3rd party libraries (dlls) now for Visual Studio 2010/C# and I've noticed that in their distributions \bin directory they usually have two versions Debug and Release.

Is there a way to add these libraries as references to the project, but use the Release build (when I'm building a release), and use the Debug build (when I'm debugging)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

番薯 2024-11-04 08:49:06
<Reference Include="MyLib">
   <HintPath>..\lib\$(Configuration)\MyLib.dll</HintPath>
</Reference>
<Reference Include="MyLib">
   <HintPath>..\lib\$(Configuration)\MyLib.dll</HintPath>
</Reference>
友谊不毕业 2024-11-04 08:49:06

您可以编辑 csproj 文件,手动设置包含引用的 ItemGroup 上的 Condition 属性。

  <ItemGroup Condition="'$(Configuration)' == 'Debug'">
    <Reference Include="MyLib">
      <HintPath>..\..\Debug\MyLib.dll</HintPath>
    </Reference>
  </ItemGroup>

  <ItemGroup Condition="'$(Configuration)' == 'Release'">
    <Reference Include="MyLib">
      <HintPath>..\..\Release\MyLib.dll</HintPath>
    </Reference>
  </ItemGroup>

请参阅本文了解更多信息。

You can edit the csproj file manually set the Condition attribute on the ItemGroup containing the reference.

  <ItemGroup Condition="'$(Configuration)' == 'Debug'">
    <Reference Include="MyLib">
      <HintPath>..\..\Debug\MyLib.dll</HintPath>
    </Reference>
  </ItemGroup>

  <ItemGroup Condition="'$(Configuration)' == 'Release'">
    <Reference Include="MyLib">
      <HintPath>..\..\Release\MyLib.dll</HintPath>
    </Reference>
  </ItemGroup>

See this article for a bit more information.

短叹 2024-11-04 08:49:06

如果您使用发布文件夹和调试文件夹,那么 WaffleSouffle 的答案绝对是最好的,正如原始问题所述。

似乎还有另一个选项不太明显,因为 VS (VS2010) 在编辑 csproj 文件时不会在 IntelliSense 中显示它。

您可以将条件添加到 HintPath 元素。像这样:

<Reference Include="MyLib">      
      <HintPath Condition="'$(Configuration)'=='Release'">..\lib\MyLib.dll</HintPath>
      <HintPath Condition="'$(Configuration)'=='Debug'">..\lib\Debug\MyLib.dll</HintPath>
</Reference>

我在 找到了 Vivek Rathod 描述上述方法的文章http://blog.vivekrathod.com/2013/03/conditionally-referencing-debug-and.html

我在以下位置检查了项目文件的 XMS 架构文件:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild\Microsoft.Build.Core.xsd
和:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild\Microsoft.Build.Commontypes.xsd

我看不到 Condition 是 HintPath 元素支持的属性,但它似乎确实有效......

编辑1:
这不会使引用在 Visual Studio 中显示两次,这是已接受答案的问题。

编辑2:
实际上,如果您完全省略 HintPath,Visual Studio 将在项目输出文件夹中查找。所以你实际上可以这样做:

<Reference Include="MyLib">        
     <!-- // Removed HintPath, VS looks for references in $(OutDir) --> 
</Reference> 

搜索顺序在文件 Microsoft.Common.targets
中指定
看:
Visual Studio 中的 HintPath 与 ReferencePath

The answer by WaffleSouffle is definitely the best if you use a Release- and a Debug-folder, as the original question states.

There seems to be another option that is not so obvious because VS (VS2010) does not show it in the IntelliSense when editing the csproj-file.

You can add the condition to the HintPath-element. Like this:

<Reference Include="MyLib">      
      <HintPath Condition="'$(Configuration)'=='Release'">..\lib\MyLib.dll</HintPath>
      <HintPath Condition="'$(Configuration)'=='Debug'">..\lib\Debug\MyLib.dll</HintPath>
</Reference>

I found an article by Vivek Rathod describing the above approach at http://blog.vivekrathod.com/2013/03/conditionally-referencing-debug-and.html.

I checked the XMS Schema file for the project file at:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild\Microsoft.Build.Core.xsd
and:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild\Microsoft.Build.Commontypes.xsd

I cannot see that Condition is a supported attribute for the HintPath-element, but it does seem to work.....

EDIT 1:
This does not make the reference show up twice in Visual Studio which is an issue with the accepted answer.

EDIT 2:
Actually, if you omit the HintPath alltogether Visual Studio will look in the projects output folder. So you can actually do this:

<Reference Include="MyLib">        
     <!-- // Removed HintPath, VS looks for references in $(OutDir) --> 
</Reference> 

The search order is specified in the file Microsoft.Common.targets
See:
HintPath vs ReferencePath in Visual Studio

心碎的声音 2024-11-04 08:49:06

是的,但可能不是 VS2010 本身的。您可以编辑 .csproj 文件并使用条件属性来创建对发布或调试的引用。

<Reference Include="MyLib" Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  <HintPath>..\lib\Debug\MyLib.dll</HintPath>
</Reference>

或者

<Reference Include="MyLib" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
  <HintPath>..\lib\Release\MyLib.dll</HintPath>
</Reference>

Yes, but probably not natively inside VS2010. You can edit the .csproj file and use Condition attributes to create the references to Release or Debug.

<Reference Include="MyLib" Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  <HintPath>..\lib\Debug\MyLib.dll</HintPath>
</Reference>

or

<Reference Include="MyLib" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
  <HintPath>..\lib\Release\MyLib.dll</HintPath>
</Reference>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文