• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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);
        }
    }

 

 

 

代码下载


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#三层架构之系统的登录验证与添加数据的实现发布时间:2022-07-10
下一篇:
C#多线程学习笔记-3发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap