C#面向对象方式设置、读取应用配置
2023-09-11 14:16:54 时间
关注点:
- 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接口是为了让界面绑定配置项时,配置项的值发生变化时界面刷新显示。
单看这篇文章是看不明白的,需要大伙移步原文看看。
相关文章
- C#.Net 如何动态加载与卸载程序集(.dll或者.exe)6-----在不卸载程序域的前提下替换程序集文件。
- C#如何使用和开发自定义配置节
- C#面向服务编程技术WCF从入门到实战演练
- 《C#高级编程》学习笔记----c#内存管理--栈VS堆
- C#.NET常见问题(FAQ)-如何将cs文件编译成dll文件 exe文件 如何调用dll文件
- 浅析Java与C#的事件处理机制
- 重学c#系列——非托管实例(五)
- [工具] 将Sublime Text 3配置为C#代码编辑器
- [转] 基于C#的波形显示控件的实现
- C# Oracle、Sql Server连接(增、删、改、查)
- Atitit.面向接口的web 原理与设计重写 路由启动绑定配置url router rewriting urlpage mvc mvp的 java c#.net php js
- Atitit 文件上传 架构设计 实现机制 解决方案 实践java php c#.net js javascript c++ python
- c#使用selenium+Chromedriver参数配置
- (五十二)c#Winform自定义控件-LED数字-HZHControls
- c# 执行python方法
- [C#]c#中数据的同步加锁机制 的几种方法