【问题标题】:load and replace dll application加载和替换 dll 应用程序
【发布时间】:2015-08-07 02:48:21
【问题描述】:

我有 WPF 应用程序 AppTest。

DllMaster1 - 这只是一个带有控件的窗口。

在主应用程序中引用了一个类库DllMaster1。

Window app = new DllMaster1.MainWindow();
app.ShowDialog();
app = null;
GC.Collect();

现在我需要从内存中删除一个对象,这样我就可以用另一个对象替换他,因为当我想替换 DllMaster1 时,我会收到以下消息:

操作无法完成,因为其中的文件夹或文件是 在另一个程序中打开。

【问题讨论】:

标签: c# wpf dll garbage-collection


【解决方案1】:

一旦将 Dll 加载到 AppDomain 中,如果不卸载整个 AppDomain,就无法卸载它。您不能释放主 AppDomain,因为它拥有正在执行的应用程序。解决方案是创建一个新的 AppDomain 并在其中加载 dll。完成后,您可以卸载第二个 AppDomain,这将释放 dll 上的锁定。

您可以在这里找到代码:Using AppDomain in C# to dynamically load and unload dll

请记住,这不是一件简单的事情;有很多复杂性和微妙的行为可能会导致一些非常有趣的错误。

【讨论】:

    【解决方案2】:
            AppDomain appDomain = AppDomain.CreateDomain("appDomain");
            Assembly a = appDomain.Load("DllMaster1");
            Type myType = a.GetType("DllMaster1.MainWindow");
            MethodInfo myMethod = myType.GetMethod("MyMethod");
            object obj = Activator.CreateInstance(myType);
            myMethod.Invoke(obj, null);
            AppDomain.Unload(appDomain);
    

    MyMethod 是 DllMaster1 this.Show(); 中的新方法。卸载不工作,我仍然无法删除dll。

    【讨论】:

      猜你喜欢
      • 2015-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多