本来一直用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() { } }}