有可能用“弱”替换对强命名程序集的引用吗?参考? [英] Is it possible to replace a reference to a strongly-named assembly with a "weak" reference?

查看:94
本文介绍了有可能用“弱”替换对强命名程序集的引用吗?参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个需要SQL Server SMO库的.NET工具。我不在乎它是Server 2005(9.0),2008(10.0)还是2008 R2的版本(可能是10.5,未选中)。 SMO库与SQL Server一起安装,因此我可以安全地假定在安装了SQL Server的任何系统上,也可以使用某些版本的SMO库。

I'm writing a .NET tool that requires the SQL Server SMO library. I don't care if it's the version from Server 2005 (9.0), 2008 (10.0) or 2008 R2 (probably 10.5, didn't check). The SMO library is installed together SQL Server, so I can safely assume that on any system with SQL Server installed, some version of the SMO library is available as well.

不幸的是,则SMO库是全名的:如果我在项目中添加了对SMO 9.0的引用,则如果客户系统上仅存在SMO 10.0,它将失败( FileNotFoundException

Unfortunately, the SMO libraries are strongly-named: If I add a reference to SMO 9.0 in my project, it will fail (FileNotFoundException) if only SMO 10.0 is present on the customer's system, and vice versa.

是否可以通过某种方式告诉编译器该库的任何版本对我来说是否合适?还是我真的必须分发3个相同版本的工具,每个版本都编译为不同版本的SMO?

Is there some way to tell the compiler that any version of the library is fine for me? Or do I really have to distribute 3 identical versions of my tool, each compiled to a different version of the SMO?

免责声明:我确实知道可以重新分发SMO库(以及SMO库所需的库)。但是(a)一个苗条的100KB独立EXE和(b)一个成熟的安装程序包之间有很大的区别,该安装程序包可以安装大量的先决条件。

免责声明2:我知道以下重复项:

  • c# - can you make a "weak" assembly reference to a strong named assembly
  • Need a C# Assembly to reference a strongly named assembly loosely

但是,提供的解决方案不适合。在问题1中,开发人员可以控制引用的DLL(我没有)。在问题2中,开发人员可以控制目标系统(我也不是)。

推荐答案

我知道无法删除对确切版本的依赖。这就是强名称存在的原因之一-避免版本不匹配。程序集的内部或什至公共接口可以在版本之间更改,并且您会发现新版本与旧版本不向后兼容。因此,.NET会寻找在编译期间使用的版本,以确保应用程序能够正常运行。

As I know it is not possible to remove the dependency on exact version. That is one of reasons why strong names exist - to avoid version mismatch. Internals or even public interfaces of the assembly can change among version and you can find that new version is not backward compatible with the old one. Because of that .NET looks for version used during compilation to make sure that application works correctly.

如果第三方认为其新版本向后兼容并且是否部署了程序集他们可以向GAC添加发布者策略,它将自动重定向。

If third party decides that their new version is backward compatible and if they deploy assembly to GAC they can add publisher policy which will do redirect automatically.

如果您决定要强制加载另一个程序集,则可以使用@chibacity提到的方法或为 AppDomain.CurrentDomain.AssemblyResolve实现处理程序。当.NET无法找到引用的程序集时,将触发此事件,并且您可以通过调用 Assembly.LoadFrom 来实现自己的逻辑来查找并加载它。在这种情况下,完全取决于您加载哪个版本。

If you decide that you want to force loading another assembly you can use the approach mentioned by @chibacity or implement handler for AppDomain.CurrentDomain.AssemblyResolve. This event fires when .NET is not able to find referenced assembly and you can implement your own logic to find it and load it by calling Assembly.LoadFrom. In such case it is completely up to you which version you load.

这篇关于有可能用“弱”替换对强命名程序集的引用吗?参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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