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