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:缺少对象在其他文章中复述

  • 相关阅读:

    jQuery post使用变量作参数名
    线性结构____二叉堆
    Java虚拟机
    spring在线生成
    树形结构_红黑树:平衡2X 哈夫曼树:最优2X
    线性结构____双链表/栈/队列
    Java中的String,StringBuilder,StringBuffer三者的区别
    JPress的CMS系统在Window下的部署和使用
    List之contains方法
  • 原文地址:https://www.cnblogs.com/bridgew/p/12709072.html
Copyright © 2011-2022 走看看