private void SetRemoteLoaderObject(string dllName) { AppDomainSetup setup = new AppDomainSetup(); // 启用影像复制程序集,将准备加载的程序集拷贝一份至CachePath,防止锁定 setup.ShadowCopyFiles = "true"; domain = AppDomain.CreateDomain(dllName, null, setup); // dll通过路径解析失败的话, AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; domains.Add(dllName, domain); object[] parms = { dllName }; BindingFlags bindings = BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public; try { rl = (Seasky.AssemblyLoader.RemoteLoader)domain.CreateInstanceFromAndUnwrap( "Seasky.AssemblyLoader.dll", "Seasky.AssemblyLoader.RemoteLoader", true, bindings, null, parms, null, null); } catch (Exception ex) { throw ex; } }
public void Unload(string dllName) { if (domains.ContainsKey(dllName)) { AppDomain appDomain = (AppDomain)domains[dllName]; AppDomain.Unload(appDomain); domains.Remove(dllName); AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve; GC.Collect(); domain = null; rl = null; } }