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

  • 相关阅读:
    分工的好处
    这是真正的随笔,无论形式还是内容。。。
    javascript 检查函数传入的实参个数和期待的形参个数是否相同的方法
    JS中比较难记的函数
    3.0 Basic Usage of Class
    0.0 Visual Studio
    2.0 Exercise of Basic Statements
    5.0 String And RegularExpressions.
    1.0 HelloWorld
    dell mini9 fluxbox 声卡的安装 和 屏幕分辨率和刷新率的调节
  • 原文地址:https://www.cnblogs.com/bridgew/p/12709072.html
Copyright © 2011-2022 走看看