有没有办法根据 Visual Studio 中的构建配置指定程序集引用?

发布于 2024-08-13 02:21:21 字数 204 浏览 4 评论 0原文

我有一个项目,通过 API 为另一个应用程序添加了一些可扩展性。但是,我希望能够将同一个项目用于其应用程序的多个版本,因为大多数代码是相同的。

但是,应用程序的每个版本都需要引用该版本软件的正确程序集。他们将程序集加载到 GAC 中,因此即使我可以根据构建配置指定要使用的程序集的版本,我也可以。有没有办法从 VS 内部执行此操作,或者我是否需要外部构建工具?

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.

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?

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

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

发布评论

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

评论(2

零時差 2024-08-20 02:21:21

有一种方法可以做到这一点,但您必须手动编辑项目文件。项目文件的许多元素(包括引用元素)中都可以应用一个 Condition 属性。

您可以将这些添加到引用中以指定何时应使用引用:

<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)每个参考将仅包含在相关选择的构建配置中。

将其与代码中的条件编译符号和 #if 语句结合起来,您应该能够执行您想要的操作。

如果执行此操作,需要注意的一点是,Visual Studio 很容易从项目文件中删除条件属性。

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>

You then define several build configurations (V1, V2, V3) and each reference will be included only in the relevant chosen build configuration.

Combine this with conditional compilation symbols and #if statements in your code and you should be able to do what you want.

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.

风启觞 2024-08-20 02:21:21
<Reference Include="log4net, Version=1.2.11.0, Culture=neutral, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\SharedLibs\log4net\$(Platform)\$(Configuration)\log4net.dll</HintPath>
</Reference>

您可以使用以下属性替换提示路径:

$(Configuration) 相当于发布/调试或您拥有的任何其他配置。
$(Platform) 相当于 x86/x64/Any CPU

如果您的配置包括 Any CPU 那么您需要在 $(Configuration) 两边加上单引号

另请参阅 adrianbanks 引用的条件选项

<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

If your configuration includes Any CPU then you will need to put single quotes around $(Configuration)

Also refer to the condition options referenced by adrianbanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文