博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
app.config自定义配置节点
阅读量:7287 次
发布时间:2019-06-30

本文共 5712 字,大约阅读时间需要 19 分钟。

本来一直用xml保存配置文件的,但有一个组件就写一个好麻烦.于是想起了自定义配置节点哈哈~~我撒娇了复习了下

首先我在ConfigManager.Instance使用单例模式,其次ReflectionCollection属性定义上使用IsRequired = false都是为了集中部署配置节点,比如也许

新程序不需要映射功能可以不写这个节点~~嘿嘿热烈的笑脸

一先看看配置文件

二代码

using System;using System.Collections.Generic;using System.Text;using System.Configuration;using LiuJia.Config.Reflection;namespace LiuJia.Config{    ///     /// 配置节点集合    ///    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;         ///         /// 单例模式,默认取 name="LiuJia"的节点数据        ///        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()        {        }                 ///         /// 映射配置节点数据集合------------------二级节点名称定义Reflection        ///         [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{    ///     /// 映射配置集合类Reflection节点定义    ///     public sealed class ReflectionConfigElementCollection : ConfigurationElementCollection    {        private const string Const_Info = "Info";        ///         /// 说明        ///         [ConfigurationProperty(Const_Info, IsRequired = false, IsKey = true)]        public string Info        {            get            {                return (string)this[Const_Info];            }            set            {                this[Const_Info] = value;            }        }        public ReflectionConfigElementCollection() :base()        {         }        ///         /// 创建一个节点类        ///         /// 
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{ /// /// 引用配置的节点类配置 最下层Add节点 /// 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"; /// /// 要反射的类名,带命名空间 /// [ConfigurationProperty(Const_ClassName, IsRequired = true, IsKey = true)] public string ClassName { get { return (string)this[Const_ClassName]; } set { this[Const_ClassName] = value; } } /// /// DLLName是反射的时候DLL的名称 /// [ConfigurationProperty(Const_DLLName, IsRequired = true, IsKey = true)] public string DLLName { get { return (string)this[Const_DLLName]; } set { this[Const_DLLName] = value; } } /// /// 文件存放地址,如果为空则返回 /// AppDomain.CurrentDomain.BaseDirectory /// 文件的跟目录 /// [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; } } /// /// 说明 /// [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

你可能感兴趣的文章
CentOS7.3安装Python3.6
查看>>
怎么才能用ABBYY FineReader提高工作效率
查看>>
STORM 落入MONGO速度优化
查看>>
python:守护进程deamon
查看>>
coding项目怎样和其他人共享
查看>>
Android wifi 设置相关
查看>>
vue中一个关于input元素的小坑
查看>>
oracle避免约束带来的导入数据解决方案
查看>>
多行文本字段运行时展示成单行文本
查看>>
sharepoint 禁用使用资源管理器打开
查看>>
jquery iframe弹出多选框
查看>>
记某个客户不能通过HTTPS访问在AWS部署站点的问题
查看>>
[Voice Tips 2] IPHONE
查看>>
Ubuntu Server版安装Gnome图形桌面
查看>>
360抢夺“度娘”?
查看>>
我的友情链接
查看>>
firewall-cmd防火墙概述
查看>>
Redhat+Nginx0.8.46+PHP5.2.14+Mysql5.1.46构建LNMP(X64)平台
查看>>
win8 体验
查看>>
LAMP环境搭建图形界面配置MySQL数据库
查看>>