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

发布了31 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/breakbridge/article/details/102976530