c#动态加载dll
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Reflection; 5 using System.IO; 6 7 namespace Alif.CommonAPI.DynamicLoadAssembly 8 { 9 public class AssemblyDynamicLoader<T> 10 { 11 private AppDomain appDomain; 12 13 private DynamicRemoteLoadAssembly<T> remoteLoader; 14 15 public T InvokeMethod(string assemblyName, string assemblyPath, string assemblyConfigFilePath, string fullClassName, string methodName, params object[] args) 16 { 17 AppDomainSetup setup = new AppDomainSetup(); 18 setup.ApplicationName = "ApplicationLoader"; 19 setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory + @"bin\"; 20 //setup.PrivateBinPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "private"); 21 setup.CachePath = setup.ApplicationBase; 22 setup.ShadowCopyFiles = "true"; 23 if (assemblyConfigFilePath != string.Empty) 24 { 25 setup.ConfigurationFile = AppDomain.CurrentDomain.BaseDirectory + assemblyConfigFilePath; 26 } 27 setup.ShadowCopyDirectories = setup.ApplicationBase; 28 setup.LoaderOptimization = LoaderOptimization.SingleDomain; 29 30 this.appDomain = AppDomain.CreateDomain("ApplicationLoaderDomain", null, setup); 31 String name = Assembly.GetExecutingAssembly().GetName().FullName; 32 33 this.remoteLoader = (DynamicRemoteLoadAssembly<T>)this.appDomain.CreateInstanceAndUnwrap(name, typeof(DynamicRemoteLoadAssembly<T>).FullName); 34 35 assemblyName = AppDomain.CurrentDomain.BaseDirectory + assemblyPath + assemblyName; 36 37 return this.remoteLoader.InvokeMethod(assemblyName, fullClassName, methodName, args); 38 } 39 40 /// <summary> 41 /// 42 /// </summary> 43 public void Unload() 44 { 45 try 46 { 47 AppDomain.Unload(this.appDomain); 48 this.appDomain = null; 49 } 50 catch (CannotUnloadAppDomainException ex) 51 { 52 53 } 54 } 55 } 56 }
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Reflection; 5 using System.Globalization; 6 7 namespace Alif.CommonAPI.DynamicLoadAssembly 8 { 9 public class DynamicRemoteLoadAssembly<T> : MarshalByRefObject 10 { 11 private Assembly assembly = null; 12 13 public T InvokeMethod(string assemblyPath, string fullClassName, string methodName, params object[] args) 14 { 15 this.assembly = null; 16 T result = default(T); 17 try 18 { 19 this.assembly = Assembly.LoadFile(assemblyPath); 20 Type pgmType = null; 21 if (this.assembly != null) 22 { 23 pgmType = this.assembly.GetType(fullClassName, true, true); 24 } 25 else 26 { 27 pgmType = Type.GetType(fullClassName, true, true); 28 } 29 BindingFlags defaultBinding = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.InvokeMethod | BindingFlags.Static; 30 CultureInfo cultureInfo = new CultureInfo("es-ES", false); 31 try 32 { 33 MethodInfo methisInfo = assembly.GetType(fullClassName, true, true).GetMethod(methodName); 34 35 if (methisInfo == null) 36 { 37 new Exception("EMethod does not exist!"); 38 } 39 40 if (methisInfo.IsStatic) 41 { 42 if (methisInfo.GetParameters().Length == 0) 43 { 44 if (methisInfo.ReturnType == typeof(void)) 45 { 46 pgmType.InvokeMember(methodName, defaultBinding, null, null, null, cultureInfo); 47 } 48 else 49 { 50 result = (T)pgmType.InvokeMember(methodName, defaultBinding, null, null, null, cultureInfo); 51 } 52 } 53 else 54 { 55 if (methisInfo.ReturnType == typeof(void)) 56 { 57 pgmType.InvokeMember(methodName, defaultBinding, null, null, args, cultureInfo); 58 } 59 60 else 61 { 62 result = (T)pgmType.InvokeMember(methodName, defaultBinding, null, null, args, cultureInfo); 63 } 64 } 65 } 66 else 67 { 68 69 if (methisInfo.GetParameters().Length == 0) 70 { 71 object pgmClass = Activator.CreateInstance(pgmType); 72 if (methisInfo.ReturnType == typeof(void)) 73 { 74 pgmType.InvokeMember(methodName, defaultBinding, null, pgmClass, null, cultureInfo); 75 } 76 else 77 { 78 result = (T)pgmType.InvokeMember(methodName, defaultBinding, null, pgmClass, null, cultureInfo); 79 } 80 } 81 else 82 { 83 object pgmClass = Activator.CreateInstance(pgmType); 84 if (methisInfo.ReturnType == typeof(void)) 85 { 86 pgmType.InvokeMember(methodName, defaultBinding, null, pgmClass, args, cultureInfo); 87 } 88 else 89 { 90 result = (T)pgmType.InvokeMember(methodName, defaultBinding, null, pgmClass, args, cultureInfo); 91 } 92 } 93 } 94 } 95 catch (Exception e) 96 { 97 result = (T)pgmType.InvokeMember(methodName, defaultBinding, null, null, null, cultureInfo); 98 } 99 return result; 100 } 101 catch (Exception ee) 102 { 103 return result; 104 } 105 } 106 } 107 }
人生,总是有一些空城旧事,年华未央;总是有些季节,一季花凉,满地忧伤。许多事,看开了,便会峰回路转;许多梦,看淡了,便会云开日出。学会思索,学会珍藏,微笑领悟,默默坚强。