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”。于是对文章中的类进行了修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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 @   数据酷软件  阅读(413)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示