Пример #1
0
        static void Main(string[] args)
        {
            // 因为GetExecutingAssembly获取的当前函数执行的exe/dll的程序名,所以不能放到工具类中,不然获得的永远是工具类的名称
            string        strAssemblyName = Assembly.GetExecutingAssembly().Location.Substring(Assembly.GetExecutingAssembly().Location.LastIndexOf("\\"));
            Configuration config          = ConfOperator.GetConfig(strAssemblyName); // 设置了该程序(exe/dll)的配置文件路径

            string strChannelsPath = ConfOperator.GetPath(config.AppSettings.Settings["ChannelPath"].Value);

            // 遍历指定文件夹下的通道文件夹,必须以.conf结尾
            foreach (string s in Directory.GetDirectories(strChannelsPath, "*.conf"))
            {
                BackgroundWorker bgwChannel = new BackgroundWorker();
                bgwChannel.DoWork             += bgwChannel_DoWork;
                bgwChannel.RunWorkerCompleted += bgwChannel_RunWorkerCompleted;
                bgwChannel.RunWorkerAsync(s);
            }
            Console.ReadKey();
        }
Пример #2
0
        public static Channel CreateChannel(string strChannelName)
        {
            string        strAssemblyName = Assembly.GetExecutingAssembly().Location.Substring(Assembly.GetExecutingAssembly().Location.LastIndexOf("\\"));
            Configuration config          = ConfOperator.GetConfig(strAssemblyName);

            Channel objChannel = new Channel(strChannelName);

            string strProtocalName = GetProtocalName(config, strChannelName);
            // 根据配置文件中的协议名称动态加载同名的dll,利用反射生成同名的协议类
            string    strDLLPath      = ConfOperator.GetPath(config.AppSettings.Settings["DLLPath"].Value) + "\\" + strProtocalName + ".dll";
            Assembly  objAssembly     = Assembly.LoadFrom(strDLLPath);
            Type      objProtocalType = (from g in objAssembly.GetTypes() where g.Name == strProtocalName select g).First();
            IProtocal objProtocal     = Activator.CreateInstance(objProtocalType) as IProtocal;

            objChannel.SetProtocal(objProtocal);


            return(objChannel);
        }