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