visual-studio 有没有办法根据 Visual Studio 中的构建配置指定程序集引用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1786917/
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
Is there a way to specify assembly references based on build configuration in Visual Studio?
提问by snicker
I have a project that adds some extensibility to another application through their API. However, I want to be able to use the same project for multiple versions of their application, because most of the code is the same.
我有一个项目,通过他们的 API 为另一个应用程序添加了一些可扩展性。但是,我希望能够将同一个项目用于其应用程序的多个版本,因为大部分代码都是相同的。
However, each version of the application requires a reference to the proper assembly for that version of the software. They load their assemblies into the GAC, so even if I could specify the versionof the assembly to use based on build configuration I would be fine. Is there a way to do this from inside of VS or do I need an external build tool?
但是,应用程序的每个版本都需要引用该版本软件的正确程序集。他们将他们的程序集加载到 GAC 中,因此即使我可以根据构建配置指定要使用的程序集版本,我也可以。有没有办法从 VS 内部执行此操作,或者我是否需要外部构建工具?
回答by adrianbanks
There is a way to do this, but you will have to hand edit your project files. The project files can have a Conditionattribute applied to them in many of the elements, including the one for references.
有一种方法可以做到这一点,但您必须手动编辑您的项目文件。项目文件可以Condition在许多元素中应用一个属性,包括用于引用的元素。
You can add these to your references to specify when the reference should be used:
您可以将这些添加到您的引用中以指定何时应该使用引用:
<Reference Include="Product, Version=1.0.0.0" Condition="'$(Configuration)'=='V1'">
</Reference>
<Reference Include="Product, Version=2.0.0.0" Condition="'$(Configuration)'=='V2'">
</Reference>
<Reference Include="Product, Version=3.0.0.0" Condition="'$(Configuration)'=='V3'">
</Reference>
You then define several build configurations (V1, V2, V3) and each reference will be included only in the relevant chosen build configuration.
然后定义多个构建配置(V1、V2、V3),每个引用将仅包含在相关的所选构建配置中。
Combine this with conditional compilation symbols and #ifstatements in your code and you should be able to do what you want.
将此与#if代码中的条件编译符号和语句结合使用,您应该能够做您想做的事。
A thing to be careful of if you do this is that it is easy to have Visual Studio remove the conditional attributes from the project file.
如果您这样做,需要注意的一点是很容易让 Visual Studio 从项目文件中删除条件属性。
回答by sweetfa
<Reference Include="log4net, Version=1.2.11.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\SharedLibs\log4net$(Platform)$(Configuration)\log4net.dll</HintPath>
</Reference>
You can replace the hint path with the properties:
您可以使用以下属性替换提示路径:
$(Configuration) is equivalent to Release/Debug or whatever other configuration you have. $(Platform) is equivalent to x86/x64/Any CPU
$(Configuration) 相当于 Release/Debug 或您拥有的任何其他配置。$(Platform) 相当于 x86/x64/Any CPU
If your configuration includes Any CPU then you will need to put single quotes around $(Configuration)
如果您的配置包括 Any CPU,那么您需要在 $(Configuration) 周围加上单引号
Also refer to the condition options referenced by adrianbanks
另请参阅 adrianbanks 引用的条件选项

