配置文件_自定義section標籤獲取數據

  前言:爲了節約時間,先只粘貼關鍵代碼:ide

1-添加section標籤,name爲自定義標籤名稱,type爲:命名空間+類型,程序集名稱優化

<section name="watchModel" type="DataCommon.Help.WatchModel,DataCommon" />

2-自定義標籤數據:this

  watchModel爲自定義標籤(ConfigurationSection),watchItems爲自定義標籤的數據集(ConfigurationElementCollection);add爲數據集裏的model(ConfigurationElement)。spa

<watchModel>
    <watchItems>
      <!--上   班-->
      <add ID="1" IsEnable="true" BeginTime="09:05:00" EndTime="09:15:00" MaxActionTimes="2" ActionSeconds="120" ActionName="SendTipsToDingding" ActionData="shangban" />
      <!--下   班-->
      <add ID="2" IsEnable="true" BeginTime="17:50:00" EndTime="18:05:00" MaxActionTimes="2" ActionSeconds="120" ActionName="SendTipsToDingding" ActionData="xiaban" />
      <!--每日BUG-->
      <add ID="3" IsEnable="true" BeginTime="09:10:00" EndTime="09:15:00" MaxActionTimes="1" ActionSeconds="0" ActionName="MyProjectBugTips" ActionData="" />
      <!--吃飯提醒-->
      <add ID="4" IsEnable="true" BeginTime="11:35:00" EndTime="11:40:00" MaxActionTimes="2" ActionSeconds="120" ActionName="SendTipsToDingding" ActionData="chifan" />
      <!--項目上線臨時時間-->
      <add ID="5" IsEnable="true" BeginTime="14:05:00" EndTime="17:15:00" MaxActionTimes="10" ActionSeconds="30" ActionName="MyProjectBugTips" ActionData="bugCheck" />
    </watchItems>
  </watchModel>

3-建立自定義標籤Model:code

標籤分爲3部分,代碼也對應3個繼承類:ConfigurationSection,ConfigurationElementCollection,ConfigurationElement。對象

類的屬性和標籤屬性使用:ConfigurationProperty("標籤屬性")進行對應,須要對get,set方法進行改造。blog

集合標籤:須要對key,createElement,和下標獲取對象方法,進行重構。繼承

 

常見錯誤-1-對象watchModel須要繼承ConfigrationSection,總之每一個子標籤對應的model都須要繼承對應的屬性,並對其進行改寫或重寫:ip

建立 watchModel 的配置節處理程序時出錯: 類型「DataCommon.Help.WatchModel」不從「System.Configuration.IConfigurationSectionHandler」繼承。element

 

public class ConfigHelper{
    /// <summary>
        /// 獲取Section對象數據集
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T GetSectionT<T>(string sectionName) where T : class
        {
            T t = ConfigurationManager.GetSection(sectionName) as T;
            return t;
        }
}

 

WatchModel watchModel = ConfigHelper.GetSectionT<WatchModel>("watchModel");

 

namespace DataCommon.Help
{
    public class WatchModel : ConfigurationSection
    {
        [ConfigurationProperty("watchItems")]
        public WatchItems WatchItems
        {
            get
            {
                return this["watchItems"] as WatchItems;
            }
            set
            {
                this["watchItems"] = value;
            }
        }
    }

    public class WatchItems : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new WatchItem();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((WatchItem)element).ID;
        }

        public WatchItem this[object id]
        {
            get
            {
                return (WatchItem)base.BaseGet(id);
            }
        }
    }

    public class WatchItem : ConfigurationElement
    {
        /// <summary>
        /// 惟一標識
        /// </summary>
        [ConfigurationProperty("ID")]
        public int ID
        {
            get
            {
                return (int)this["ID"];
            }
            set
            {
                this["ID"] = value;
            }
        }
        /// <summary>
        /// 是否啓用
        /// </summary>
        [ConfigurationProperty("IsEnable")]
        public bool IsEnable
        {
            get
            {
                return (bool)this["IsEnable"];
            }
            set
            {
                this["IsEnable"] = value;
            }
        }
        /// <summary>
        /// 開始時間(偏差1秒=取決於計時器默認時間間隔)
        /// </summary>
        [ConfigurationProperty("BeginTime")]
        public string BeginTime
        {
            get
            {
                return (string)this["BeginTime"];
            }
            set
            {
                this["BeginTime"] = value;
            }
        }
        /// <summary>
        /// 結束時間
        /// </summary>
        [ConfigurationProperty("EndTime")]
        public string EndTime
        {
            get
            {
                return (string)this["EndTime"];
            }
            set
            {
                this["EndTime"] = value;
            }
        }
        /// <summary>
        /// 最大執行次數
        /// </summary>
        [ConfigurationProperty("MaxActionTimes")]
        public int MaxActionTimes
        {
            get
            {
                return (int)this["MaxActionTimes"];
            }
            set
            {
                this["MaxActionTimes"] = value;
            }
        }
        /// <summary>
        /// 計時週期內執行的動做(動做會在到達開始時間後的)
        /// </summary>
        [ConfigurationProperty("ActionName")]
        public string ActionName
        {
            get
            {
                return (string)this["ActionName"];
            }
            set
            {
                this["ActionName"] = value;
            }
        }
        /// <summary>
        /// 計時週期內執行的動做傳入數據(動做會在到達開始時間後的)
        /// </summary>
        [ConfigurationProperty("ActionData")]
        public string ActionData
        {
            get
            {
                return (string)this["ActionData"];
            }
            set
            {
                this["ActionData"] = value;
            }
        }
        /// <summary>
        /// 動做執行時間間隔(秒)
        /// </summary>
        [ConfigurationProperty("ActionSeconds")]
        public int ActionSeconds
        {
            get
            {
                return (int)this["ActionSeconds"];
            }
            set
            {
                this["ActionSeconds"] = value;
            }
        }
    }
}

 

總結:以上就是主要的代碼了,中間也遇到過一些問題,上面基本上都寫了,之後再補充優化吧。

相關文章
相關標籤/搜索