![](/img/trans.png)
[英]WCF Service Reference in Visual Studio 2022 does not include project references
[英]Visual Studio Project: How to include a reference for one configuration only?
信封:VS2008 C#項目
我需要構建可在2種不同環境中使用的應用程序。 在這些環境之一中,我需要使用第三方DLL程序集。
我可以使用#if塊隔離使用此DLL的代碼。 但是,如何在CS項目文件中有條件地包含對DLL的引用?
編輯:womp在他的評論中有一個很好的觀點。 我轉過一個單獨的問題 :如果從未調用過引用的DLL,是否會完全加載它? TIA,
卸載項目並以.XML格式打開
找到參考項標簽並添加一個Condition屬性。
例如:
<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>
注意,最后一個引用現在有一個條件。
我知道這是一篇過時的文章,但是如果其他任何人在找到答案之前都找到了它,就像我一樣,就是這樣:您需要在項目文件中使用“ Choose”元素:
您可以在一個地方定義條件引用和條件編譯,因此您不必在代碼中使用#if。
它可以在SharpDevelop中工作,並且由於它是MS的文檔,所以我認為它可以在Visual Studio中工作。
以下是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>
受到此處顯示的問題和答案的啟發,您可以在要有條件運行的零件周圍添加<Choose>
和<When Condition>
命令。 例如:
<Choose>
<When Condition="$(USEDLL) == true">
<ItemGroup>
<EmbeddedResource Include="test.dll">
<LogicalName>test.dll</LogicalName>
</EmbeddedResource>
</ItemGroup>
</When>
</Choose>
然后在CLI中,只需使用MSBuild中的/p
屬性,如下所示:
MSBuild "C:\myproject\myproject.sln" /p:USEDLL=true
...或者如果您不想要DLL,只需:
MSBuild "C:\myproject\myproject.sln" /p:USEDLL=false
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.