C#面向对象方式设置、读取应用配置

关注点

  • 1、用面向对象方式的方式(get,set)访问和设置配置项
  • 2、“CallerMemberName”在.net 4以下的变通方式

最后一周了,大伙都进入过年模式了。身还在,心已远。最近事情不是很多,老是看闪存也是不对啊。于是想起来前几天看到的一篇文章:“在 .NET中,一种更方便操作配置项的方法”;于是跃跃欲试,尝试一下。结果遇到点问题,文章中使用了CallerMemberName属性来简化存取配置项时需要硬编码带上配置项的Key的问题,这个属性的用途就是标记在运行时自动获取属性名,但是这是.net 4.5以上才有的。由于我们做工控需要兼容工控机的老操作系统,.net一直是4.0。于是遇到兼容问题了。百度出来,发现杨中科老师的办法是使用StackTrace具体就是:“new StackTrace(true).GetFrame(1).GetMethod().Name”。于是对文章中的类进行了修改:

 public abstract class ConfigSetting : INotifyPropertyChanged
    {
        private void NotifyPropertyChanged(string property)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
        }
        /// <summary>
        /// 配置类
        /// </summary>
        /// <param name="configuration">配置</param>
        public ConfigSetting(Configuration configuration)
        {
            Configuration = configuration;
        }

        /// <summary>
        /// 当前配置
        /// </summary>
        public Configuration Configuration
        {
            get;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 获取当前程序配置
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public static Configuration GetCurrentConfiguration()
        {
            return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        }

        /// <summary>
        /// 返回指定的配置项的值
        /// </summary>
        /// <param name="settingKey"></param>
        /// <returns></returns>
        protected virtual string GetSettingValue(string settingKey = null)
        {
            if (string.IsNullOrEmpty(settingKey))
                settingKey = new StackTrace(true).GetFrame(1).GetMethod().Name.Replace("get_", "");            
            return Configuration?.AppSettings?.Settings[settingKey]?.Value;
        }

        /// <summary>
        /// 返回指定的配置项的值
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="settingKey"></param>
        /// <returns></returns>
        protected virtual T GetSettingValue<T>(string settingKey = null)
        {
            if (string.IsNullOrEmpty(settingKey))
                settingKey = new StackTrace(true).GetFrame(1).GetMethod().Name.Replace("get_", "");
            var value = GetSettingValue(settingKey);
            if (string.IsNullOrWhiteSpace(value))
            {
                return default(T);
            }
            else
            {
                return (T)Convert.ChangeType(value, typeof(T));
            }
        }

        /// <summary>
        /// 为指定的配置项设置值
        /// </summary>
        /// <param name="value"></param>
        /// <param name="settingKey"></param>
        protected virtual void SetSettingValue(object value, string settingKey = null)
        {
            if (string.IsNullOrEmpty(settingKey))
                settingKey = new StackTrace(true).GetFrame(1).GetMethod().Name.Replace("set_", "");
            Configuration.AddOrUpdateAppSettingItem(settingKey, value?.ToString());
            NotifyPropertyChanged(settingKey);
            Configuration.Save();
        }

        /// <summary>
        /// 返回指定的配置项的值
        /// </summary>
        /// <param name="settingKey"></param>
        /// <returns></returns>
        public string GetSettingValueByKey(string settingKey)
        {
            return GetSettingValue(settingKey);
        }

        /// <summary>
        /// 返回指定的配置项的值
        /// </summary>
        /// <param name="settingKey"></param>
        /// <returns></returns>
        public T GetSettingValueByKey<T>(string settingKey)
        {
            return GetSettingValue<T>(settingKey);
        }

        /// <summary>
        /// 为指定的配置项设置值
        /// </summary>
        /// <param name="value"></param>
        /// <param name="settingKey"></param>
        public void SetSettingValueByKey(string settingKey, object value)
        {
            SetSettingValue(value, settingKey);
            NotifyPropertyChanged(settingKey);
        }
    }

  实现INotifyPropertyChanged接口是为了让界面绑定配置项时,配置项的值发生变化时界面刷新显示。

单看这篇文章是看不明白的,需要大伙移步原文看看。

 

posted @ 2018-02-05 17:20  数据酷软件  阅读(406)  评论(0编辑  收藏  举报