使用相同 NuGet 包的两个不同版本 [英] Using two different versions of same the NuGet package

查看:46
本文介绍了使用相同 NuGet 包的两个不同版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用两个不同版本的相同库(OpenCVSharp 2.x 和 OpenCVSharp 3.x).

I wanted to use two different version same library (OpenCVSharp 2.x and OpenCVSharp 3.x).

我将这两个包都下载到了单独的项目(我们称之为 OCV2Wrapper 和 OCV3Wrapper)并在我的项目中引用了这两个包装器.我不得不从一个包 (2.x) 中重命名库并参考它们手册,因为:我们可以在 NuGet 中添加 2 个不同版本的同一包.我阅读了外部别名,并在其中一个包装器(在我的情况下为 2.x)中使用了外部别名.但我有一些主要问题:

I downloaded those two packages both to the separate project (let's call it OCV2Wrapper and OCV3Wrapper) and reference both wrappers in my project. I had to renamed libraries from one package (2.x) and reference them manual because: Can we add 2 different versions of same package in NuGet. I read about external aliases and I used external alias in one of the wrappers (2.x in my case). But I have some major problems:

  • 我重命名的库没有复制到启动项目构建(引用两个包装器的那个),而是在 2.x 包装器的构建中
  • 它不起作用,因为它说即使我从 2.x 包装器手动复制重命名的库,它也无法从我的 2.x 包装器中找到类型.

在 C# 中这个场景的正确方法是什么?

What is the correct approach for this scenario in C#?

我想在解决方案中同时使用这两个包装器,因为 2.x 版本包含算法(SIFT 和 SURF),3.x 版本包含算法(Kaze 和 AKaze).

I want to use both wrappers in solution because the 2.x version contains algorithms (SIFT and SURF) and 3.x version contains algorithms (Kaze and AKaze).

我可以接受这两个包来自 NuGet 以外的地方,但我更喜欢 3.x 来自 NuGet 并且 2.x 版本是手动配置的.

I can live that with both packages coming from somewhere other than NuGet, but I prefer that 3.x comes from NuGet and the 2.x version is manually configured.

推荐答案

如前所述,引用 NuGet 包的 2 个不同版本并没有错,只要这些引用是在不同的 Visual Studio 项目中进行的.

As already stated, there is nothing wrong with referencing 2 different versions of a NuGet package, as long as it's in different Visual Studio Projects that those references are made.

但这也是简单部分结束的地方,但我认为还有一些选择.根据您的需要,我看到了以下选项.

But this is also where the easy part ends, but I think there are a few options left. Depending on your needs, I see the following options.

  1. 创建一个后期构建步骤,将多版本程序集注册到 GAC 中.只要每个程序集具有不同的程序集版本,CLR 就会在需要时从 GAC 中选择正确的程序集.

  1. Create a post build step which registers the multi-versioned assemblies into the GAC. As long as each assembly have different assembly version, the CLR will pick up the right assembly from the GAC when needed.

创建一个构建后步骤,将不同的程序集复制到应用程序 bin 文件夹的子文件夹中,例如 bin/package-v1bin/package-v2.然后,您可以在您的应用程序中覆盖 AssemblyResolve 事件,如下所述:https://msdn.microsoft.com/en-us/library/ff527268(v=vs.110).aspx.这将使您可以在需要时加载正确版本的程序集.

Create a post build step which copies the different assemblies into a subfolder of your application bin folder like bin/package-v1 and bin/package-v2. Then you can, in your application, override the AssemblyResolve event as described here: https://msdn.microsoft.com/en-us/library/ff527268(v=vs.110).aspx. This will make it possible for you to load the assembly in the right version at the time of need.

如果您不想使用 AssemblyResolve,那么您还可以修改您的 web/app.config 以执行程序集重定向/探测作为此处描述:https://msdn.microsoft.com/en-us/library/4191fzwb(v=vs.110).aspx

If you don't want to play around with AssemblyResolve, then you can also modify your web/app.config to do assembly redirect/probing as described here: https://msdn.microsoft.com/en-us/library/4191fzwb(v=vs.110).aspx

希望对大家有所帮助,下次不用再修改第三方源代码了.

Hope this helps a bit, so you don't have to modify third party source code next time.

这篇关于使用相同 NuGet 包的两个不同版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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