C# MEF不处理加载的程序集

C# MEF不处理加载的程序集,c#,loading,mef,.net-assembly,dispose,C#,Loading,Mef,.net Assembly,Dispose,我使用MEF从本地目录加载程序集,它工作正常。稍后,当我想替换其中一个加载的程序集时,我收到一个异常,表示该文件已被另一个进程使用。这是我的密码: var moduleList = _service.GetModules(); List<Module> modules = null; string modulesDirPath = Path.Combine(Environment.GetFolderPath(Environment.Sp

我使用MEF从本地目录加载程序集,它工作正常。稍后,当我想替换其中一个加载的程序集时,我收到一个异常,表示该文件已被另一个进程使用。这是我的密码:

        var moduleList = _service.GetModules();
        List<Module> modules = null;
        string modulesDirPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"PortailModules");

        if (!Directory.Exists(modulesDirPath))
            Directory.CreateDirectory(modulesDirPath);

        using (GenericModuleLoader<IModule> loader = new GenericModuleLoader<IModule>(modulesDirPath))
        {
            modules = new List<Module>(
                      from p in loader.Plugins
                      select new Module
                      {
                          FullPath = p.FullPath,
                          AssemblyName = p.AssemblyName,
                          Version = p.Version
                      });                
        }
当复制到文件中时,我收到异常


有什么想法吗?

您无法从AppDomain卸载程序集@SLaks的意思是通常您无法卸载程序集。唯一的例外是,您可以卸载整个AppDomains。但是,线程与AppDomains绑定,因此您不太可能设计一个程序在运行时完全部分卸载程序集。您可以设置卷影复制,或从字节数组加载程序集。
foreach (var module in moduleList)
            {
                var file = new FileInfo(module.FullPath);
                if (!modules.Any(x => x.AssemblyName == module.AssemblyName))
                {
                    File.Copy(module.FullPath, Path.Combine(modulesDirPath, file.Name), true);
                }
                else
                {
                    if(CompareVersion(module.Version, modules.First(x => x.Name == module.Name).Version) == 1)
                    {
                        File.Copy(module.FullPath, Path.Combine(modulesDirPath, file.Name), true);
                    }
                }
            }