.NET dll热插拔,没有应用程序重启

Tom*_*ini 12 c# dll dynamic reload

假设您在.NET(C#)中具有以下情况:

namespace MyDll
{
    public class MyClass
    {
        public string GetValue()
        {
            return "wrong value";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这段代码编译成一个dll说,MyDll.Dll.

然后你有一个名为MyApplication.exe的应用程序,使用MyDll.dll作为参考,创建一个MyClass类的实例并调用方法GetValue:

MyClass instance = new MyClass();
instance.GetValue();
Run Code Online (Sandbox Code Playgroud)

一旦你意识到MyClass.GetValue()的当前实现是错误的,有什么办法可以修复MyClass.GetValue()这样的方法

namespace MyDll
{
    public class MyClass
    {
        public string GetValue()
        {
            return "correct value";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

和HOT交换生成的MyDll.dll,而不重新启动MyApplication.exe ???

stackoverflow和google中提出的所有解决方案都无法正常工作,因为即使MyDll.dll加载到为此目的创建的新AppDomain上,当我卸载调用时也是如此

AppDomain.Unload(anoterAppDomainJustForMyDll);
Run Code Online (Sandbox Code Playgroud)

它返回没有错误,但如果我尝试用更正的一个覆盖原始的MyDll.dll(当MyApplication.exe仍在运行时),我收到错误"无法覆盖因为另一个进程正在使用的dll"....

Tom*_*ini 5

问题由我自己解决:请参考codeplex中的文章

对我来说,能够在不重新启动应用程序的情况下热交换新的 dll 很重要,正如文章所建议的,这是可以做到的,因为这是通过应用程序本身完成的。我之前的尝试失败的原因是我试图从外部覆盖目标 dll(在这种情况下是在资源管理器中)。但是,如果覆盖是按照建议的解决方案完成的,那么从应用程序本身来看,它会按预期工作。

我需要在应用程序方面多做一点来定义可以部署可版本化 ddls 的目录,但这对我来说是完全可以接受的。