C# 加载同一程序集的不同版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/58035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 10:15:12  来源:igfitidea点击:

Loading different versions of the same assembly

提问by Gulzar Nazim

Using reflection, I need to load 2 different versions of the same assembly. Can I load the 2 versions in 2 different AppDomains in the same process?

使用反射,我需要加载同一个程序集的 2 个不同版本。我可以在同一进程中在 2 个不同的 AppDomains 中加载 2 个版本吗?

I need to do some data migration from the old version of the app to the new version. Please let me know if this is possible or should I use 2 separate processes.

我需要从旧版本的应用程序到新版本进行一些数据迁移。请让我知道这是否可行,或者我应该使用 2 个单独的进程。

采纳答案by Gulzar Nazim

UPDATE: I thought I will post my findings as an answer. Reflection proved too complex in terms of development effort, tracking run time errors etc. I remember doing a different approach using 2 different processes when faced with a similar situation long time back (Thank you Brandon).

更新:我想我会发布我的发现作为答案。事实证明,反射在开发工作、跟踪运行时错误等方面过于复杂。我记得很久以前遇到类似情况时,我使用 2 个不同的进程做了不同的方法(谢谢 Brandon)。

This is the plan: Nothing elegant but easier in terms of development and troubleshooting. Since it is a one time job, we just have to make it work.

这是计划:在开发和故障排除方面,没有什么优雅但更容易。由于这是一项一次性工作,我们只需要让它发挥作用。

Host a remoting process (which i call the server) having the new version of the application. A remoting client has references for the older version.

托管具有新版本应用程序的远程处理进程(我称之为服务器)。远程客户端具有对旧版本的引用。

Remoting client instantiates and loads the objects with data required for migration. Convert the old objects into common serializable objects and pass as parameters to the server.

远程客户端使用迁移所需的数据实例化并加载对象。将旧对象转换为常见的可序列化对象并作为参数传递给服务器。

Remoting Server uses the common data to instantiate and load the new objects. Invokes the functions on the new types to persist their data.

Remoting Server 使用公共数据来实例化和加载新对象。调用新类型的函数来持久化它们的数据。

回答by GEOCHET

If you are doing it at design time (which you indicate you are not) this should help you:

如果您是在设计时这样做(您表示不是),这应该对您有所帮助:

http://blogs.msdn.com/abhinaba/archive/2005/11/30/498278.aspx

http://blogs.msdn.com/abhinaba/archive/2005/11/30/498278.aspx

If you are doing it dynamically through reflection (looks like the case here) this might help you:

如果您通过反射动态地执行此操作(看起来像这里的情况),这可能会对您有所帮助:

https://www.infosysblogs.com/microsoft/2007/04/loading_multiple_versions_of_s.html

https://www.infosysblogs.com/microsoft/2007/04/loading_multiple_versions_of_s.html