C#动态加载DLL,通过配置文件实现对程序集的即插即用

C#动态加载DLL,通过配置文件实现对程序集的即插即用

大概介绍下思想和使用的技术

1,需要加载的dll均为一个类库

2,每个类均实现一个接口(即这些类有相同的共有方法)

3,使用xml配置文件

4,自动同步最新的xml文件

5,使用dictionary

 

实现逻辑

1,程序开始运行后,加载初始的所有任务

2,根据每个任务,创建相应的对象,并把这些对象保存在dictionary中

3,当用户请求一个任务时候,去dictionary中根据任务名称(dictionary的key)找到相应的类

4,调用该类执行相应的方法

5,若需要新的任务(此任务不包含在任务dictionary中),只需要更新xml文件,程序会重读xml文件加载最新的任务到dictionary中,保证程序在运行状态可以增减新的任务。

 

    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, IBaseJob> runningJobList = GetJobList();
            Console.ForegroundColor = ConsoleColor.Green;
            Show(" Please input the job name which you want to do,\r\n Input 'C' can get the last version job.");
            Console.ResetColor();
            while (true)
            {
                try
                {
                    string content = Console.ReadLine().ToLower();

                    if (content.ToLower() == "c")
                    {
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        Show("Compare Started,Job count: " + runningJobList.Count);
                        runningJobList = GetJobList();
                        Show("Compare Completed,Job count: " + runningJobList.Count);
                        Console.ResetColor();
                    }
                    else
                    {
                        if (content.Length >= 2)
                        {
                            DoJob(content, runningJobList, new Random().Next());
                        }
                    }
                }
                catch (Exception e)
                {
                    Show(e.Message);
                }
                Thread.Sleep(1000);
            }
        }

        static Dictionary<string, IBaseJob> GetJobList()
        {
            Dictionary<string, string> dictJobs = LoadXML();
            Dictionary<string, IBaseJob> jobList = new Dictionary<string, IBaseJob>();
            IBaseJob baseJob = null;
                foreach (string key in dictJobs.Keys)
                {
                    baseJob = LoadAssembly(key, dictJobs[key]);
                    if (baseJob != null)
                    {
                        jobList.Add(key.Replace("JobProvider", "").ToLower(), baseJob);
                    }
                }
            return jobList;
        }

        static Dictionary<string, string> LoadXML()
        {
            Dictionary<string, string> dictJobs = new Dictionary<string, string>();
            XmlDocument xml = new XmlDocument();
            xml.Load("ProviderConfig.xml");
            XmlNodeList xmlList = xml.SelectNodes("ProviderInformation");
            foreach (XmlNode node in xmlList)
            {
                XmlNodeList childNodes = node.ChildNodes;
                foreach (XmlNode childNode in childNodes)
                {
                    dictJobs.Add(childNode.ChildNodes[0].InnerXml, childNode.ChildNodes[1].InnerXml);
                }
            }
            return dictJobs;
        }

        static IBaseJob LoadAssembly(string ddlName, string className)
        {
            IBaseJob baseJob = null;
            Assembly jobAssembly = null;
            jobAssembly = Assembly.Load(ddlName);
            if (jobAssembly != null)
            {
                object tmpobj = jobAssembly.CreateInstance(className);
                if (tmpobj != null && tmpobj is IBaseJob)
                {
                    baseJob = tmpobj as IBaseJob;
                }
            }
            return baseJob;
        }

        static void DoJob(string key, Dictionary<string, IBaseJob> runningJobList, object objPara)
        {
            try
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Show(runningJobList[key].DoTempJob(objPara).ToString());
                Console.ResetColor();
            }
            catch (KeyNotFoundException)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Show("The job '" + key + "' you want not exsit in job list.");
                Console.ResetColor();
            }
            catch (Exception e)
            {
                throw e;
            }

        }

        static void Show(string information)
        {
            Console.WriteLine(information);
        }
    }

 

 

 

代码下载

posted @ 2011-04-11 13:08  Kevin.Cheung  阅读(3177)  评论(0编辑  收藏  举报