zoukankan      html  css  js  c++  java
  • C# 以枚举名为Section,枚举值为 Key 读取 ini 中的Value

        /// <summary>
        /// 以枚举名称为Section,枚举值为Key的ini配置文件的读写
        /// </summary>
        public class IniFileOperate : IniRW
        {
    
            protected Dictionary<object, string> _DefaultParam = new Dictionary<object, string>();
    
            public IniFileOperate(string path)
                : base(path)
            {
            }
    
            public bool AddDefaultValue<T>(T key, string value)
            {
                _DefaultParam[key] = value;
                return true;
            }
    
            public bool WriteValue<T>(T key, object value)
            {
                string section = key.GetType().Name;
                return WriteValue(section, key.ToString(), value);
            }
    
            public string ReadString<T>(T key)
            {
                string section = key.GetType().Name;
                return ReadString(section, key.ToString());
            }
    
            public override string ReadString(string section, string key)
            {
                string ret = base.ReadString(section, key);
                if (ret == string.Empty)
                {
                    KeyValuePair<object, string> keyValue = _DefaultParam.FirstOrDefault(v => v.Key.ToString() == key);
                    if (keyValue.Key != null && keyValue.Value != null)
                    {
                        object k = keyValue.Key;
                        string value = _DefaultParam[k];
                        WriteValue(k, value);
                        ret = value;
                    }
                }
                return ret;
            }
    
            public int ReadInt<T>(T key)
            {
                return (int)ReadDouble(key);
            }
    
            public short ReadShort<T>(T key)
            {
                return (short)ReadDouble(key);
            }
    
            public byte ReadByte<T>(T key)
            {
                return (byte)ReadDouble(key);
            }
    
            public double ReadDouble<T>(T key)
            {
                string section = key.GetType().Name;
                return ReadDouble(section, key.ToString());
            }
    
            public bool ReadBool<T>(T key)
            {
                string section = key.GetType().Name;
                return ReadBool(section, key.ToString());
            }
    
            public DateTime ReadDateTime<T>(T key)
            {
                string section = key.GetType().Name;
                string time = ReadString(section, key.ToString());
                DateTime dt = DateTime.MinValue;
                DateTime.TryParse(time, out dt);
                return dt;
            }
    
        }
    

     ps:缺少对象在其他文章中复述

  • 相关阅读:
    HttpRuntime.Cache的使用经验
    js 字符串中取得第一个字符和最后一个字符
    CSAPP笔记-第一章
    共和党减税法案的个人减税
    bash学习进行中
    建站日志
    Python学习进行中
    Check your data! 数据预处理血泪教训
    bash
    【python技巧系列】在循环中处理异常并继续运行
  • 原文地址:https://www.cnblogs.com/bridgew/p/12709072.html
Copyright © 2011-2022 走看看