在运行时加载 2 个版本的程序集 [英] Loading 2 versions of assembly at runtime

查看:30
本文介绍了在运行时加载 2 个版本的程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几周我一直在尝试破解这个问题,但还没有找到好的解决方案;希望我能在这里得到答案.

I've been trying to crack this one over the last couple of weeks and have not found a good solution yet; hopefully I can get an answer here.

我有两个程序集(ZA & ZB),它们都指向一个公共项目/dll(ZC),但可能是不同的版本(即相同的 dll 名称,相同的命名空间,某些类可能不同).每个程序集都自行工作,但是,如果一个程序集在运行时由另一个程序集加载(例如 A 加载 B),那么我无法让它工作.需要一些帮助.

I have two assemblies (ZA & ZB), both of which point to a common project/dll (ZC) but which could be on a different version (i.e. same dll name, same namespaces, some classes may be different). Each assembly works by itself, however, if one is loaded by the other at runtime (e.g. A loads B), then I cannot get it to work. Need some help.

设置如下:

  • ZA 依赖于 ZC(通用)1.1 版
  • ZB 依赖于 ZC 1.0 版

ZA 需要加载需要在运行时在 ZB(这取决于 ZC)中加载一些东西.

ZA needs to load needs to load something in ZB (which depends on ZC), at runtime.

ZA 是主应用程序.在其 bin 目录下,有一个插件目录 plugins/plugin-ZB,我想将所有 ZB 及其依赖项 (ZC) 放在其中.

ZA is the master app. Under its bin directory, there's a plugins directory plugins/plugin-ZB under which I would like to place all of ZB and its dependencies (ZC).

这是我迄今为止尝试过的:

Here's what I've tried so far:

Assembly.Load() 使用相同版本的 dll - 工作正常.

Assembly.Load() using same version of dll - worked fine.

Assembly.Load() 使用不同版本的 dll - ZB 加载,但是当方法运行时,我得到一个方法未找到异常.

Assembly.Load() using different versions of dll - ZB loads, but when the method runs, I get a method not found exception.

AppDomain.Load() 出现文件未找到错误;我什至使用委托来解析程序集.

AppDomain.Load() got a file not found error; I even used the delegate to resolve assemblies.

关于 ZC 的一些细节:- 有些方法是公共静态的(有些不是).例如.Log.Log("hello");- 有些可能返回值(原语或对象).- 一些方法是非静态的(和返回值).

Some details regarding ZC: - some methods are public static (some are not). E.g. Log.Log("hello"); - some may return values (primitives or objects). - some methods are non static (and return values).

帮助?- TIA

推荐答案

    m_Assembly1 = Reflection.Assembly.LoadFile(IO.Path.Combine(System.Environment.CurrentDirectory, "Old Version\Some.dll"))
    m_Assembly2 = Reflection.Assembly.LoadFile(IO.Path.Combine(System.Environment.CurrentDirectory, "New Version\Some.dll"))

    Console.WriteLine("Old Version: " & m_Assembly1.GetName.Version.ToString)
    Console.WriteLine("New Version: " & m_Assembly2.GetName.Version.ToString)

    m_OldObject = m_Assembly1.CreateInstance("FullClassName")
    m_NewObject = m_Assembly2.CreateInstance("FullClassName")

从现在开始,我使用后期绑定和/或反射来运行我的测试.

From here on out I used late binding and/or reflection to run my tests.

.NET:加载相同的两个版本DLL

这篇关于在运行时加载 2 个版本的程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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