.net dll热插拔

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using System.IO;
using System.Reflection;

namespace ConsoleApplication30
{
    public class Program
    {
        static void Main(string[] args)
        {
            
            string fileName =@"E:\test\ClassLibrary1.dll";
            string className = "ClassLibrary1.Class1";

            Program pm = (new Program());

            JobBase z = pm.LoadFromAssembly(fileName, className, "sdfd");
            pm.Unload();
            Console.WriteLine("1");
            Console.Read();
            JobBase z1 = pm.LoadFromAssembly(fileName, className, "sdfd1");
            pm.Unload();
            Console.WriteLine("2");
            Console.Read();
            JobBase z2 = pm.LoadFromAssembly(fileName, className, "sdfd2");
            pm.Unload();
            Console.WriteLine("3");
            int mz2 = 0;
        }

        AppDomain domain = null;
        public JobBase LoadFromAssembly(string fileName, string className, string key)
        {
            
            if (domain == null)
            {
                AppDomainSetup setup = new AppDomainSetup
                {
                    ApplicationName = "AssemblyCache",
                    CachePath =
                        Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                    ShadowCopyFiles = "true"
                };
                domain = AppDomain.CreateDomain("AutoJobServiceAppDomain_Ver12"null, setup);
                //RunningDomainDict[key] = domain;
            }
            var obj = (JobBase)domain.CreateInstanceFromAndUnwrap(fileName, className);
            //InitDomainInfo();

            return obj;
        }
        public void Unload()
        {
            AppDomain.Unload(domain);
        }


        public void load(string fileName, string className)
        {
            Assembly asm = Assembly.LoadFrom(fileName);
       
                object o = asm.CreateInstance(className);
                JobBase rc = (JobBase)o;

              
        }
    }
}
posted @ 2012-08-13 19:42  烈马狂生  阅读(562)  评论(0编辑  收藏  举报