c# - 如何释放通过Reflection加载的DLL?

标签 c# reflection

String dlls = ConfigurationManager.AppSettings["ApiFolder"];
FileInfo[] files = new DirectoryInfo(dlls).GetFiles("*.dll");
foreach (FileInfo file in files) {
    if (!LoadedApis.ContainsKey(file.Name)) {
        Assembly ass = Assembly.LoadFrom(dlls + file.Name);
        foreach (Type t in ass.GetTypes()) {
            if (t.GetTypeInfo().FindInterfaces((x, y) => true, null).Contains(typeof(IApi))) {
                IApi api = (IApi)ass.CreateInstance(t.FullName);
                LoadedApis.Add(file.Name, api);
            }
        }
    }
}

我使用上面的代码将具有特定接口(interface)的所有 DLL 从文件夹加载到字典中,以便稍后访问它们。我正在开发一个插件系统,您所要做的就是放入一个 DLL,它将被加载和使用。

它工作得很好,但我想解释一个我目前不知道该怎么做的用例。我想允许删除或更新 DLL。

是否可以释放已通过反射加载的DLL,以便将其删除或覆盖?我想我必须进行一些互操作调用,但我不知道要进行哪些调用。

或者,采用不同的方法,我可以将 DLL 加载到内存中,这样我就不会从一开始就锁定该文件吗?

最佳答案

There is no way to unload an individual assembly without unloading all of the application domains that contain it. Use the Unload method from AppDomain to unload the application domains. For more information, see How to: Unload an Application Domain.

来源:https://msdn.microsoft.com/en-us/library/ms173101.aspx

关于c# - 如何释放通过Reflection加载的DLL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28633347/

相关文章:

javascript - 是否可以在没有点击的情况下通过 selenium webdriver 执行 OnClick javascript 函数

c# - 使用构造的泛型类型中的 'ConstructorInfo',如何从开放类型中获取匹配的 'ConstructorInfo'?

c# - 使用 jQuery 从 ASP.Net JSON 服务获取数据

c# - 如何将文件路径数组转换为分层 JSON 结构

c# - Type.GetType(string typeName) 返回 null

Php Hack中使用反射获取泛型类型

java - 通过反射从java执行编译的groovy脚本

reflection - 在 Kotlin 中按名称动态获取函数

c# - 如何在不知道使用 Linq to XML 嵌套多深的情况下查找元素?

C# Google.ProtocolBuffers 反序列化方法 (proto3)