动态添加dll
string callingDomainName = AppDomain.CurrentDomain.FriendlyName; AppDomain ad = AppDomain.CreateDomain("DLL Unload test"); ProxyObject obj = (ProxyObject)ad.CreateInstanceAndUnwrap(Assembly.GetAssembly(typeof(ProxyObject)).FullName, typeof(ProxyObject).ToString()); obj.LoadAssembly(); obj.Invoke("Test.T1", "BB", null); AppDomain.Unload(ad); obj = null;
Assembly.LoadFrom会自动去寻找所依赖的其他的dll
public class ProxyObject : MarshalByRefObject { Assembly assembly = null; public void LoadAssembly() { assembly = Assembly.LoadFrom(@"C:SkyWindTest1.dll"); assembly = Assembly.LoadFrom(@"C:SkyWindTest.dll"); } public bool Invoke(string fullClassName, string methodName, params Object[] args) { if (assembly == null) return false; Type tp = assembly.GetType(fullClassName); if (tp == null) return false; MethodInfo method = tp.GetMethod(methodName); if (method == null) return false; Object obj = Activator.CreateInstance(tp); method.Invoke(obj, args); return true; } }