C# 将不同版本的程序集加载到同一个项目中 [英] C# Load different versions of assembly to the same project

查看:38
本文介绍了C# 将不同版本的程序集加载到同一个项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一些执行 NUnit 等操作的工具.在这个工具中,我打开 .dll 程序集并调用它的方法来运行一些测试.

I'm creating some tool what performs several operations like NUnit. Inside this tool I open .dll assembly and invoke methods form it to run some test.

一切正常,直到我需要重新加载 .dll 而不重启程序.这个想法是,当工具运行时,我们将所需的程序集复制到某个临时文件夹并从那里调用.如果我需要重新加载,我将另一个复制到另一个临时文件夹并尝试加载从另一个文件夹新复制的并加载到以前的程序集对象

Everything is going OK till the time I need to reload .dll withour program restart. The idea is that when tool is run we copy required assembly to some temporary folder and invoke from there. If I need to reload I copy another one to another temporary folder and try to load newly copied from another folder and load to previous assembly object

  ExecutingAssembly = Assembly.LoadFrom(AssemblyFullPath); 

但我的问题是,在我将 AssemblyFullPath 更改为新的并调用 Assembly.LoadFrom 后,它只返回第一次加载的旧程序集,而不是第二次加载的程序集!也许问题是我们无法加载多个不同版本的程序集?有什么解决办法?

But my problem is that after I change AssemblyFullPath to new one and call Assembly.LoadFrom it returns just old assembly what was loaded first time but not the second one! Maybe the problem is that we cannot load several assemblies with different versions? What is the solution?

推荐答案

CLR 确实支持将多个版本的强命名程序集加载到同一个 AppDomain 中.这仅在您的程序集具有强命名并且每个程序集的版本都与另一个不同的情况下才有效.

The CLR does support loading multiple versions of strongly named assemblies into the same AppDomain. This only works though if your assemblies are strongly named and each one has a different version than the other.

我猜您更有可能处理未签名的程序集.如果是这种情况,那么您所要求的实际上是不可能的.一旦给定的程序集加载到 AppDomain 中,它将一直保留在那里,直到 AppDomain 被卸载.要使其工作,您必须将加载的程序集周围的所有工作抽象到一个单独的 AppDomain 中,并为每个程序集使用一个新的 AppDomain

I'm guessing it's more likely that you are dealing with unsigned assemblies. If that is the case then what you're asking for isn't really possible. Once a given assembly is loaded into an AppDomain it will remain there until the AppDomain is unloaded. To get this to work you will have to abstract out all of the work around the loaded assemblies into a separate AppDomain and use a new AppDomain for every assembly

这篇关于C# 将不同版本的程序集加载到同一个项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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