Exemple #1
0
 public static void LoadFromFile()
 {
     using (StreamReader fs = new StreamReader(Global.ConfigFile))
     {
         Dictionary <string, IConfigIO> handleDictionary = GetConfigIOInstanceList();
         IConfigIO instance = null;
         while (!fs.EndOfStream)
         {
             string lineStr = fs.ReadLine().Trim();
             if (lineStr.Length > 0 && !lineStr.StartsWith("#"))
             {
                 if (handleDictionary.ContainsKey(lineStr))
                 {
                     instance = handleDictionary[lineStr];
                 }
                 else
                 {
                     instance?.ReadConfig(lineStr);
                 }
             }
         }
         foreach (string key in handleDictionary.Keys)
         {
             handleDictionary[key].WriteLog();
         }
     }
 }
        /// <summary>
        /// 从指定字符串中加载配置
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public BaseConfig LoadFromString(string data)
        {
            if (string.IsNullOrEmpty(data))
            {
                throw new Exception("LoadFromString参数为为空");
            }
            AppConfig config = new AppConfig();
            Dictionary <string, IConfigIO> handleDictionary = GetConfigIOInstanceList(config);
            IConfigIO instance = null;

            ReadToEnd(data, reader =>
            {
                string lineStr = reader.ReadLine().Trim();
                if (lineStr.Length > 0 && !lineStr.StartsWith("#"))
                {
                    if (handleDictionary.ContainsKey(lineStr))
                    {
                        instance = handleDictionary[lineStr];
                    }
                    else
                    {
                        instance?.ReadConfig(lineStr);
                    }
                }
            });
            foreach (string key in handleDictionary.Keys)
            {
                handleDictionary[key].WriteLog();
            }

            return(config);
        }
Exemple #3
0
        private AppConfig DoLoadFromStream(StreamReader fs)
        {
            AppConfig config = new AppConfig();
            Dictionary <string, IConfigIO> handleDictionary = GetConfigIOInstanceList(config);
            IConfigIO instance = null;

            while (!fs.EndOfStream)
            {
                string lineStr = fs.ReadLine().Trim();
                if (lineStr.Length > 0 && !lineStr.StartsWith("#"))
                {
                    if (handleDictionary.ContainsKey(lineStr))
                    {
                        instance = handleDictionary[lineStr];
                    }
                    else
                    {
                        instance?.ReadConfig(lineStr);
                    }
                }
            }
            foreach (string key in handleDictionary.Keys)
            {
                handleDictionary[key].WriteLog();
            }
            return(config);
        }
        public bool RemoveItem <T>(T item)
        {//处理器字典<处理器名称,处理器实例>
            Dictionary <string, IConfigIO> handleDictionary = GetConfigIOInstanceList(new AppConfig());
            //当前处理器实例
            IConfigIO instance = null;
            //配置文件内容缓存
            List <string> configTemp = new List <string>();
            //传入的配置是否已存在于配置文件
            bool isMatched = false;

            //分析当前配置文件内容
            ReadToEnd(reader =>
            {
                string lineStr = reader.ReadLine().Trim();

                /*
                 * 条件一:注释内容
                 * 条件二:处理器切换标识
                 * 条件三:没有处理器则不处理内容
                 */
                if (lineStr.StartsWith("#") || handleDictionary.ContainsKey(lineStr) || instance == null)
                {
                    //只有切换处理器时需要处理数据
                    if (handleDictionary.ContainsKey(lineStr))
                    {
                        instance = handleDictionary[lineStr];
                    }
                }
                else
                {
                    object tItem = instance.ReadConfig(lineStr);
                    //匹配端口映射项
                    isMatched = MatchPortMapItem(tItem as PortMapItem, item as PortMapItem);
                }
                if (isMatched && !reader.EndOfStream)
                {
                    //如果已经找到了指定配置,则不再解析后面的数据
                    configTemp.Add(reader.ReadToEnd());
                }
                else
                {
                    //未匹配上的数据,将内容原样存入缓存
                    configTemp.Add(lineStr);
                }
            });
            if (!isMatched)
            {
                if (item as PortMapItem != null)
                {
                    SavePortMapItem(item as PortMapItem, configTemp, handleDictionary);
                }
            }
            else
            {
                //匹配上了,将数据保存到文件
                ForeachWrite(configTemp);
            }
            return(true);
        }
        /// <summary>
        /// 将指定字符串解析为指定对象
        /// </summary>
        /// <param name="handlerName">处理器名称</param>
        /// <param name="str">被解析的字符串</param>
        /// <returns></returns>
        public object ParseToObject(string handlerName, string str)
        {
            Dictionary <string, IConfigIO> handleDictionary = GetConfigIOInstanceList(new AppConfig());

            if (handleDictionary.ContainsKey(handlerName))
            {
                IConfigIO handler = handleDictionary[handlerName];
                return(handler.ReadConfig(str));
            }
            else
            {
                throw new NotSupportedException($"未找到对应的处理器 {handlerName}");
            }
        }
Exemple #6
0
        public static ConfigCenter LoadFromString(string data)
        {
            ConfigCenter config = new ConfigCenter();

            if (string.IsNullOrEmpty(data))
            {
                throw new Exception("LoadFromString参数为为空");
            }
            using (MemoryStream ms = new MemoryStream())
            {
                IConfigIO    instance = null;
                StreamWriter sw       = new StreamWriter(ms);
                sw.Write(data);
                sw.Flush();
                StreamReader sr = new StreamReader(ms);
                sr.BaseStream.Position = 0;
                config = DoLoadConfig(sr);
                ms.Close();
            }
            return(config);
        }
Exemple #7
0
        /// <summary>
        /// 保存指定配置项
        /// </summary>
        /// <typeparam name="T">配置项类型</typeparam>
        /// <param name="item">要保存的配置项</param>
        /// <returns></returns>
        public bool SaveItem <T>(T item)
        {
            //处理器字典<处理器名称,处理器实例>
            Dictionary <string, IConfigIO> handleDictionary = GetConfigIOInstanceList(new AppConfig());
            //当前处理器实例
            IConfigIO instance = null;
            //配置文件内容缓存
            List <string> configTemp = new List <string>();
            //传入的配置是否已存在于配置文件
            bool isMatched = false;

            //分析当前配置文件内容
            ReadToEnd(reader =>
            {
                string lineStr = reader.ReadLine().Trim();

                /*
                 * 条件一:注释内容
                 * 条件二:处理器切换标识
                 * 条件三:没有处理器则不处理内容
                 */
                if (lineStr.StartsWith("#") || handleDictionary.ContainsKey(lineStr) || instance == null)
                {
                    //只有切换处理器时需要处理数据
                    if (handleDictionary.ContainsKey(lineStr))
                    {
                        instance = handleDictionary[lineStr];
                    }
                }
                else
                {
                    //匹配成功时,运行的方法
                    Action matchedFunc = () =>
                    {
                        configTemp.Add(instance.GetItemString(item));
                        isMatched = true;
                    };
                    object tItem = instance.ReadConfig(lineStr);
                    //匹配端口映射项
                    if (MatchPortMapItem(tItem as PortMapItem, item as PortMapItem, matchedFunc))
                    {
                    }
                    ;
                }
                if (isMatched && !reader.EndOfStream)
                {
                    //如果已经找到了指定配置,则不再解析后面的数据
                    configTemp.Add(reader.ReadToEnd());
                }
                else
                {
                    //未匹配上的数据,将内容原样存入缓存
                    configTemp.Add(lineStr);
                }
            });
            if (!isMatched)
            {
                if (item as PortMapItem != null)
                {
                    SavePortMapItem(item as PortMapItem, configTemp, handleDictionary);
                }
                else
                {
                    throw new NotSupportedException($"未找到合适的处理器用于写入配置文件 type:{item.GetType()}");
                }
            }
            return(true);
        }