有没有办法根据 Visual Studio 中的构建配置指定程序集引用? [英] Is there a way to specify assembly references based on build configuration in Visual Studio?

查看:21
本文介绍了有没有办法根据 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 中,因此即使我可以根据构建配置指定要使用的程序集的版本,我也可以.有没有办法从 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>

然后您定义多个构建配置(V1V2V3),并且每个引用将仅包含在相关选择的构建配置中.

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆