动态调用 , 卸载 Assembly

1.其实还是很麻烦的. 主要是使用起来麻烦, 你不能直接在本地调用,使用Remote Type .

2.初级资料: http://www.cnblogs.com/wayfarer/archive/2004/09/29/47896.html

3.完整的:

http://blog.csdn.net/westfruit/article/details/5501409

http://www.cnblogs.com/yuxuanji/articles/1583422.html

注意:

AppDomain 的安装是一个非常关键的部分。AssemblyProcessor 类使用 Assembly.Load() 定位程序集,它在 AppDomain 的应用程序库中寻找程序集。因此,我们需要将应用程序库设置为程序集所在的目录。这可以通过以下命令行来完成:
setup.ApplicationBase = fileInfo.DirectoryName;
还要注意86/64的问题

 class RemoteLoader : MarshalByRefObject
    {
        public RemoteLoader()
        {
           AppDomain.CurrentDomain.AssemblyResolve+=CurrentDomain_AssemblyResolve;
        }
        public void SetDomain(AppDomain arg_domain)
        {
            arg_domain.AssemblyResolve += CurrentDomain_AssemblyResolve;
        }

        Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            MessageBox.Show(args.Name);
            return null;
        }
        public Assembly assembly = null;
        public string FullName { get { return assembly.FullName; } }

        public void TestTypes(Func arg_fn)
        {
            var tps = assembly.GetTypes();
            foreach (var tp in tps)
            {
                if (arg_fn(tp))
                    return;
            }
        }
        public void LoadAssembly(string arg_strAssemblyFullName)
        {            
            assembly = Assembly.LoadFile(arg_strAssemblyFullName);
        }
        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;
        }
        public bool Compare()
        {
            var types = assembly.GetTypes();
            foreach (var type in types)
            {
                if (type.IsIncludeType(typeof(DataProviderLibrary.DataProvider)))
                    MessageBox.Show(type.ToString());
            }
            return false;
        }        
    }

    public class LocalLoader
    {
        private AppDomain appDomain;
        private RemoteLoader remoteLoader;

        public LocalLoader()
        {
            AppDomainSetup setup = new AppDomainSetup();
            setup.ApplicationName = "Test";
            setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
            setup.PrivateBinPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "private");
            setup.CachePath = setup.ApplicationBase;
            setup.ShadowCopyFiles = "true";
            setup.ShadowCopyDirectories = setup.ApplicationBase;            

            appDomain = AppDomain.CreateDomain("TestDomain", null, setup);
            string name = Assembly.GetExecutingAssembly().GetName().FullName;
            remoteLoader = (RemoteLoader)appDomain.CreateInstanceAndUnwrap(
                name,
                typeof(RemoteLoader).FullName);

            remoteLoader.SetDomain(appDomain);
        }      

        public void LoadAssembly(string fullName)
        {
            remoteLoader.LoadAssembly(fullName);
        }

        public void Unload()
        {
            AppDomain.Unload(appDomain);
            appDomain = null;
        }

        public string FullName
        {
            get
            {
                return remoteLoader.FullName;
            }
        }

        public bool Compare()
        {
            return remoteLoader.Compare(); 
        }
        public void  TestTypes(Func arg_fn)
        {
            remoteLoader.TestTypes(arg_fn);            
        }
    }  


转载于:https://www.cnblogs.com/norsd/archive/2013/02/27/6359431.html

你可能感兴趣的:(动态调用 , 卸载 Assembly)