app.config自定义配置节点
本来一直用xml保存配置文件的,但有一个组件就写一个好麻烦.于是想起了自定义配置节点哈哈~~我撒娇了复习了下
首先我在ConfigManager.Instance使用单例模式,其次ReflectionCollection属性定义上使用IsRequired = false都是为了集中部署配置节点,比如也许
新程序不需要映射功能可以不写这个节点~~嘿嘿
一先看看配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration><configSections><section name="LiuJia" type=" LiuJia.Config.ConfigManager,LiuJia" />
<!—neme=定义顶级节点的名称,type=和所使用的类.type=命名空间.类,顶级命名空间 --></configSections><LiuJia> <Reflection>
<!—ConfigManager.ReflectionCollection定义名称 –>
<!—ConfigManager.ReflectionConfigElementCollection定义集合实现--> <add DLLName="" ClassName="" FilePath="" Info="" />
<!—ConfigManager.ReflectionConfigElement 定义add的实现--> </Reflection></LiuJia> </configuration>
二代码
using System; using System.Collections.Generic; using System.Text; using System.Configuration; using LiuJia.Config.Reflection; namespace LiuJia.Config {/// <summary>/// 配置节点集合/// </summary>public sealed class ConfigManager: ConfigurationSection{private const string Const_Reflection = "Reflection";private const string Const_ReflectionConfigName = "ReflectionConfigName";private const string Const_LiuJia = "LiuJia";private static ConfigManager _Instance;private ReflectionConfigElementCollection _ReflectionCollection; /// <summary>/// 单例模式,默认取 name="LiuJia"的节点数据/// </summary>public static ConfigManager Instance{get{//---没有数据处实化if (_Instance == null){//---如果在ReflectionConfigName节点设置了名称,则走设置名称,否则走LiuJia的节点名称String ConfigName = ConfigurationManager.AppSettings[Const_ReflectionConfigName] == null ? Const_LiuJia : ConfigurationManager.AppSettings[Const_ReflectionConfigName];//--从配置节点读数据_Instance = (ConfigManager)ConfigurationManager.GetSection(ConfigName);}return _Instance;}}public ConfigManager(): base(){}/// <summary>/// 映射配置节点数据集合------------------二级节点名称定义Reflection/// </summary>[ConfigurationProperty(Const_Reflection, IsDefaultCollection = false,IsRequired=false)]public ReflectionConfigElementCollection ReflectionCollection {get{if (_ReflectionCollection == null){_ReflectionCollection = (ReflectionConfigElementCollection)base[Const_Reflection];}return _ReflectionCollection;}set{base[Const_Reflection] = value;}}}}using System; using System.Collections.Generic; using System.Text; using System.Configuration; namespace LiuJia.Config.Reflection {/// <summary>/// 映射配置集合类Reflection节点定义/// </summary>public sealed class ReflectionConfigElementCollection : ConfigurationElementCollection{private const string Const_Info = "Info";/// <summary>/// 说明/// </summary>[ConfigurationProperty(Const_Info, IsRequired = false, IsKey = true)]public string Info{get{return (string)this[Const_Info];}set{this[Const_Info] = value;}}public ReflectionConfigElementCollection() :base(){ }/// <summary>/// 创建一个节点类/// </summary>/// <returns></returns>protected override ConfigurationElement CreateNewElement() { return new ReflectionConfigElement(); }//获取对象的Key protected override object GetElementKey(ConfigurationElement element){return ((ReflectionConfigElement)element).ClassName;}//通过Key获取MyConfigElement对象 public ReflectionConfigElement GetElementByKey(string key){return (ReflectionConfigElement)base.BaseGet(key);}} }using System; using System.Collections.Generic; using System.Text; using System.Configuration; namespace LiuJia.Config.Reflection {/// <summary>/// 引用配置的节点类配置 最下层Add节点/// </summary>public sealed class ReflectionConfigElement : System.Configuration.ConfigurationElement{private const string Const_DLLName = "DLLName";private const string Const_ClassName = "ClassName";private const string Const_FilePath = "FilePath";private const string Const_Info = "Info";/// <summary>/// 要反射的类名,带命名空间/// </summary>[ConfigurationProperty(Const_ClassName, IsRequired = true, IsKey = true)]public string ClassName{get{return (string)this[Const_ClassName];}set{this[Const_ClassName] = value;}}/// <summary>/// DLLName是反射的时候DLL的名称/// </summary>[ConfigurationProperty(Const_DLLName, IsRequired = true, IsKey = true)]public string DLLName{get{return (string)this[Const_DLLName];}set{this[Const_DLLName] = value;}}/// <summary>/// 文件存放地址,如果为空则返回/// AppDomain.CurrentDomain.BaseDirectory/// 文件的跟目录/// </summary>[ConfigurationProperty(Const_FilePath, IsRequired = false, IsKey = true)]public string FilePath{get{return (string)this[Const_FilePath] == "" ? AppDomain.CurrentDomain.BaseDirectory : (string)this[Const_FilePath];}set{this[Const_FilePath] = value;}}/// <summary>/// 说明/// </summary>[ConfigurationProperty(Const_Info, IsRequired = false, IsKey = true)]public string Info{get{return (string)this[Const_Info];}set{this[Const_Info] = value;}}public ReflectionConfigElement():base(){ }} }
转载于:https://www.cnblogs.com/angellapples/p/3315720.html
app.config自定义配置节点相关推荐
- C# App.config 自定义 配置节
方法一. App.config <?xml version="1.0" encoding="utf-8" ?> <configuration& ...
- 一个web.Config或app.Config自定义段configSections的示例
一个web.Config或app.Config自定义段configSections的示例 越来越觉得,直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml ...
- ASP.NET系列:自定义配置节点的复用
appSettings太简单,为每个程序自定义配置节点太复杂,因此要解决app.config&web.config自定义配置的复用问题. 1.读取不依赖SectionName,根节点可以定义为 ...
- C# 读取app.config配置文件 节点键值,提示 配置系统未能初始化 错误的解决方案...
新建C#项目,在app.config中添加了appSettings项,运行时出现"配置系统未能初始化"的错误,MSDN里写到,如果配置文件中包含 configSections 元素 ...
- VS中C#读取app.config数据库配置字符串的三种方法(转)
关于VS2008或VS2005中数据库配置字符串的三种取法 VS2008建立Form程序时,如果添加数据源会在配置文件 app.config中自动写入连接字符串,这个字符串将会在你利用DataSet, ...
- VB.NET 在项目中添加App.config的配置
右击项目,选择属性, 在属性中选择Settings选项卡,Scope选择User 这样就会在项目中添加了App.config配置 内容: <?xml version="1.0" ...
- 它们的定义app.config中间section节点和在执行中使用
如果现在我们需要在app.config一个节点的在下面的例子中,定义,我们需要如何进行操作? <configSections><section name="integrat ...
- C#中怎样获取默认配置文件App.config中配置的键值对内容
场景 在新建一个程序后,项目中会有一个默认配置文件App.config 一般会将一些配置文件信息,比如连接数据库的字符串等信息存在此配置文件中. 怎样在代码中获取自己配置的键值对信息. 注: 博客主页 ...
- VisualStudio App.Config自动配置
在VisualStudio中,可以跟据选定的运行模式自动生成相应的配置文件: 对web类型的项目,系统已自动有了Web.config.Web.Debug.config.Web.release.conf ...
最新文章
- 笔记本的处理器型号怎么区别好坏
- 零基础学python需要多久-零基础学python大概要多久
- 【C++ 语言】Visual Studio 配置 POSIX 线程 ( Windows 不支持 POSIX | 配置文件下载 | 库文件说明 | 配置过程 )
- IDC:全球物联网支出将在2019年达到1.3万亿美元
- perl unload gbk oracle 数据库
- 慈溪微生活图标_日常生活中的图标
- [OS复习]进程互斥与同步1
- docker自动化部署
- calendR :为你定制私人专属日历
- Python进程池使用
- 淘宝/天猫收货地址列表 API和快递费用的 API解析
- PVE直通Intel核显虚拟机配置ffmpeg-qsv硬件加速
- Java 实现 n 阶行列式的求解
- 关于vue的@click传递
- Android 沉浸式Dialog弹框
- 【Cocos Creator实战教程(6)】——镜头跟随
- 最大似然估计,最大后验估计,贝叶斯估计联系与区别
- [计算机网络期末复习_例题]有限带宽、有热噪声信道的最大数据传输速率(香农定理)
- 软件测试bug不能重现,软件测试第五篇:如何提升bug重现的概率?
- html怎么添加背景图片
热门文章
- ren`guang-boss面试
- python代码转换为pytorch_Pytorch中Tensor与各种图像格式的相互转化详解
- arduino小车前进代码_基于Arduino操纵杆扩展板Funduino制作机器人小车
- Oracle对sum求和进行if,Excel中sumif函数和sumifs函数进行条件求和的异同
- matlab中的现代谱估计,请教一个Matlab关于现代功率谱估计的问题
- Ubuntu 20.04上安装Git方法
- 一名合格的运维工程师的历练之路
- JSP笔记-页面重定向
- 简单介绍基于PostgreSql 别名区分大小写的问题
- 用java建一个类_利用JAVA创建一个类。