new PropertyChangedEventArgs(property));}/// summary/// 配置类

时间:2022-06-24 05:42:08

1、用面向东西方法的方法(get,set)访谒和设置配置项

2、“CallerMemberName”在.net 4以下的变通方法

new PropertyChangedEventArgs(property));}/// summary/// 配置类

最后一周了,大伙都进入过年模式了。身还在,心已远。比来工作不是很多,老是看闪存也是不同错误啊。于是想起来前几天看到的一篇文章:“在 .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>配置</param> public ConfigSetting(Configuration configuration) { Configuration = configuration; } /// <summary> /// 当前配置 /// </summary> public Configuration Configuration { get; } public event PropertyChangedEventHandler PropertyChanged; /// <summary> /// 获取当前措施配置 /// </summary> /// <param></param> /// <returns></returns> public static Configuration GetCurrentConfiguration() { return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); } /// <summary> /// 返回指定的配置项的值 /// </summary> /// <param></param> /// <returns></returns> protected virtual string GetSettingValue(string settingKey = null) { if (string.IsNullOrEmpty(settingKey)) settingKey = new StackTrace(true).GetFrame(1).GetMethod().Name.WordStr("get_", ""); return Configuration?.AppSettings?.Settings[settingKey]?.Value; } /// <summary> /// 返回指定的配置项的值 /// </summary> /// <typeparam>值类型</typeparam> /// <param></param> /// <returns></returns> protected virtual T GetSettingValue<T>(string settingKey = null) { if (string.IsNullOrEmpty(settingKey)) settingKey = new StackTrace(true).GetFrame(1).GetMethod().Name.WordStr("get_", ""); var value = GetSettingValue(settingKey); if (string.IsNullOrWhiteSpace(value)) { return default(T); } else { return (T)Convert.ChangeType(value, typeof(T)); } } /// <summary> /// 为指定的配置项设置值 /// </summary> /// <param></param> /// <param></param> protected virtual void SetSettingValue(object value, string settingKey = null) { if (string.IsNullOrEmpty(settingKey)) settingKey = new StackTrace(true).GetFrame(1).GetMethod().Name.WordStr("set_", ""); Configuration.AddOrUpdateAppSettingItem(settingKey, value?.ToString()); NotifyPropertyChanged(settingKey); Configuration.Save(); } /// <summary> /// 返回指定的配置项的值 /// </summary> /// <param></param> /// <returns></returns> public string GetSettingValueByKey(string settingKey) { return GetSettingValue(settingKey); } /// <summary> /// 返回指定的配置项的值 /// </summary> /// <param></param> /// <returns></returns> public T GetSettingValueByKey<T>(string settingKey) { return GetSettingValue<T>(settingKey); } /// <summary> /// 为指定的配置项设置值 /// </summary> /// <param></param> /// <param></param> public void SetSettingValueByKey(string settingKey, object value) { SetSettingValue(value, settingKey); NotifyPropertyChanged(settingKey); } }

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

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