C#动态添加dll

动态添加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:\SkyWind\Test1.dll");
            assembly = Assembly.LoadFrom(@"C:\SkyWind\Test.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;
        }
    }

 

posted @ 2021-03-30 23:34  任锋  阅读(394)  评论(0编辑  收藏  举报