有没有一种方法可以在Visual Studio中基于生成配置指定程序集引用? [英] Is there a way to specify assembly references based on build configuration in Visual Studio?
问题描述
我有一个项目,可以通过其API向其他应用程序添加一些可扩展性.但是,我希望能够将同一项目用于其应用程序的多个版本,因为大多数代码是相同的.
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.
但是,应用程序的每个版本都需要引用该软件版本的正确程序集.他们将其程序集加载到GAC中,因此即使我可以根据构建配置指定要使用的程序集的 version ,也可以.是否可以从VS内部进行此操作,还是需要外部构建工具?
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 version of 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?
推荐答案
有一种方法可以执行此操作,但是您必须手动编辑项目文件.在项目文件中的许多元素中都可以应用Condition
属性,包括用于引用的元素.
There is a way to do this, but you will have to hand edit your project files. The project files can have a Condition
attribute applied to them in many of the elements, including the one for references.
您可以将这些添加到引用中,以指定何时应使用引用:
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>
然后,您定义几个构建配置(V1
,V2
,V3
),并且每个引用将仅包含在所选的相关构建配置中.
You then define several build configurations (V1
, V2
, V3
) and each reference will be included only in the relevant chosen build configuration.
将其与条件编译符号和代码中的#if
语句组合在一起,您应该可以做自己想做的事.
Combine this with conditional compilation symbols and #if
statements in your code and you should be able to do what you want.
执行此操作时要注意的一点是,Visual Studio很容易从项目文件中删除条件属性.
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中基于生成配置指定程序集引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!