using System; using System.IO; using System.Reflection; namespace PdmSwPlugin.Common { public class RemoteLoader : MarshalByRefObject { private Assembly _assembly; public void LoadAssembly(string assemblyFile) { _assembly = Assembly.LoadFrom(assemblyFile); } public dynamic GetInstance(string typeName) { if (_assembly == null) return null; var type = _assembly.GetType(typeName); var obj = Activator.CreateInstance(type); return obj; } public object ExecuteMothod(string typeName, string methodName, object[] parameters) { if (_assembly == null) return null; var type = _assembly.GetType(typeName); var obj = Activator.CreateInstance(type); // return obj; MethodInfo method = type.GetMethod(methodName); return method.Invoke(obj, parameters); } } public class PluginLoader { public AppDomain appDomain { get; set; } public RemoteLoader remoteLoader { get; set; } public PluginLoader(string pluginName) { AppDomainSetup setup = new AppDomainSetup(); setup.ApplicationName = "plugin_" + pluginName; setup.ApplicationBase = "C:\\Users\\cheng\\Documents\\Workspace\\PdmSwPlugin.MaterialSelect\\bin\\Debug";// "C:\\Users\\cheng\\Documents\\Workspace\\PdmSw\\bin\\x64\\Debug"; setup.ConfigurationFile = "C:\\Users\\cheng\\Documents\\Workspace\\PdmSw\\bin\\x64\\Debug\\PdmSwPlugin.Main.dll.config"; //"C:\\Users\\cheng\\Documents\\Workspace\\WpfUI\\bin\\x64\\Debug\\WpfUI.exe.config"; setup.CachePath = setup.ApplicationBase; //setup.ShadowCopyFiles = "true"; //setup.ShadowCopyDirectories = setup.ApplicationBase; setup.PrivateBinPath = "C:\\Users\\cheng\\Documents\\Workspace\\PdmSwPlugin.MaterialSelect\\bin\\Debug"; // AppDomain.CurrentDomain.BaseDirectory; // setup.DynamicBase = "C:\\Work\\dll\\dll"; // AppDomain.CurrentDomain.BaseDirectory; appDomain = AppDomain.CreateDomain("plugin_" + pluginName, null, setup); AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); string name = Assembly.GetAssembly(typeof(RemoteLoader)).GetName().FullName; string className = typeof(RemoteLoader).FullName; object sb = appDomain.CreateInstanceAndUnwrap(name, className); remoteLoader = (RemoteLoader)sb; //appDomain.Load("C:\\Users\\cheng\\Documents\\Workspace\\PdmSwPlugin.MaterialSelect\\bin\\Debug\\PdmSwPlugin.MaterialSelect.dll"); remoteLoader.LoadAssembly("C:\\Users\\cheng\\Documents\\Workspace\\PdmSwPlugin.MaterialSelect\\bin\\Debug\\PdmSwPlugin.MaterialSelect.dll"); } public Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { try { Assembly assembly = System.Reflection.Assembly.Load(args.Name); if (assembly != null) return assembly; } catch { // ignore load error } // *** Try to load by filename - split out the filename of the full assembly name // *** and append the base path of the original assembly (ie. look in the same dir) // *** NOTE: this doesn't account for special search paths but then that never // worked before either. string[] Parts = args.Name.Split(','); string File = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + Parts[0].Trim() + ".dll"; return System.Reflection.Assembly.LoadFrom(File); } /// /// 加载程序集 /// /// public void LoadAssembly(string assemblyFile) { remoteLoader.LoadAssembly(assemblyFile); } /// /// 创建对象实例 /// /// /// /// public dynamic GetInstance(string typeName) { if (remoteLoader == null) return null; return remoteLoader.GetInstance(typeName); } /// /// 执行类型方法 /// /// /// /// public object ExecuteMothod(string typeName, string methodName, object[] parameters) { return remoteLoader.ExecuteMothod(typeName, methodName, parameters); } /// /// 卸载应用程序域 /// public void Unload() { try { if (appDomain == null) return; AppDomain.Unload(appDomain); appDomain = null; remoteLoader = null; } catch (CannotUnloadAppDomainException ex) { throw ex; } } public Assembly[] GetAssemblies() { AppDomain a = this.appDomain; return a.GetAssemblies(); } } }