.Net設定檔——統一節點組態管理

來源:互聯網
上載者:User
對於.Net中的自訂節點,如果要單獨處理,就要為這個節點添加一個指定的類,如果我們在一個應用程式中,定義了很多個自訂節點的話,還按照這樣做,就會多出很多個處理類來,為了避免太多的類,我們將所有自定節點都指定一個自訂節點作為入口,然後唯寫一個處理類,以此為入口,讀取其他節點。

例如,設定檔只定義一個入口節點:

 <!--通用配置儲存方法-->  <traceFact type="ClassLib.ConfigManager,ClassLib">        <forum name="TraceFact.Net Community">       <root url="http:192.168.24.204"/>      <replyCount>20</replyCount>      <pageSize>30</pageSize>      <offlineTime>20</offlineTime>    </forum>        <blog>      <root url="http:asdakfj.com"/>      <urlMappings>        <rewriteRule>          <request>~/(\d{4})/Default\.aspx</request>          <sendTo>~BlogDetail.aspx?year=$1</sendTo>        </rewriteRule>      </urlMappings>    </blog>           <mailServer address="www.baidu.com" userName="lhc" password="234r32r"/>        <greetingStrategy type="ClassLib.ChineseGreeting,ClassLib"/>      </traceFact>

處理類:

 /// <summary>    /// Class GeneralConfigHandler    /// </summary>    /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 10:50:40</remarks>    public class GeneralConfigHandler:IConfigurationSectionHandler    {        /// <summary>        /// 建立配置節處理常式。        /// </summary>        /// <param name="parent">父物件。</param>        /// <param name="configContext">配置內容物件。</param>        /// <param name="section">節 XML 節點。</param>        /// <returns>建立的節處理常式對象。</returns>        /// <exception cref="System.NotImplementedException"></exception>        /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 10:50:42</remarks>        public object Create(object parent, object configContext, XmlNode section)        {            //擷取節點type屬性的值            Type t=Type.GetType(section.Attributes["type"].Value);//返回configManager            //直接將section進行傳遞            object[] paramters={section};            //將要建立的類型執行個體            object obj = null;            try            {                obj = Activator.CreateInstance(t, paramters);//使用有參數的建構函式            }            catch (Exception)            {                return null;            }            return obj;        }    }

上面的類主要是為了實現建立具體配置節點管理類,在配置節點的管理類中,存有各個子節點類的引用。

 /// <summary>    /// GeneralConfigHandler通過反射動態建立的,它實際上只是作為一個容器    ///這個類僅僅是作為一個容器,包含對其下具體節點配置的引用,並通過traceFact根節點,擷取traceFact其下的子節點,然後再建立用於映射具體的子節點的類型執行個體    /// </summary>    /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:06:21</remarks>    public class ConfigManager    {        private XmlNode section;        private FroumConfiguration forumConfig;        //對於其他節點的處理強型別訪問類也定義為成員變數放在這裡。。。。。。。        public FroumConfiguration froumConfig {            get { return forumConfig; }        }        public ConfigManager(XmlNode section) {            this.section = section;            forumConfig = new FroumConfiguration(section.SelectSingleNode("forum"));        }    }    /// <summary>    /// 具體子節點配置forum結點:處理<forum name="TraceFact.Net Community">     /// </summary>    /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:22:08</remarks>    public class FroumConfiguration{        private XmlNode forumNode;                //將forum結點傳遞進來        public FroumConfiguration(XmlNode section){            this.forumNode=section;        }        /// <summary>        /// Gets the name. <forum name="TraceFact.Net Community">         /// </summary>        /// <value>The name.</value>        /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:58:00</remarks>        public string Name{            get{                return forumNode.Attributes["name"].Value;                }        }        /// <summary>        /// Gets the root URL.:<root url="http:192.168.24.204"/>        /// </summary>        /// <value>The root URL.</value>        /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:56:08</remarks>        public string RootUrl{                    get{                return forumNode.SelectSingleNode("root").Attributes["url"].Value;            }        }        /// <summary>        /// Gets the size of the page.<pageSize>30</pageSize>        /// </summary>        /// <value>The size of the page.</value>        /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:57:18</remarks>        public int PageSize{                    get{                return int.Parse(forumNode.SelectSingleNode("pageSize").InnerText);            }        }        /// <summary>        /// Gets the reply count.<replyCount>20</replyCount>        /// </summary>        /// <value>The reply count.</value>        /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:57:03</remarks>        public int ReplyCount{                    get{                return int.Parse(forumNode.SelectSingleNode("replyCount").InnerText);            }        }        /// <summary>        /// Gets the offline time.<offlineTime>20</offlineTime>        /// </summary>        /// <value>The offline time.</value>        /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:57:35</remarks>        public int OfflineTime {            get {                return int.Parse(forumNode.SelectSingleNode("offlineTime").InnerText);            }        }    }

這樣,我們使用的時候,每次都用這一個類來實現對節點的訪問:

 ConfigManager config = ConfigurationManager.GetSection("traceFact") as ConfigManager;            //取值            ltrName.Text = config.froumConfig.Name;            ltrOfflineTime.Text = config.froumConfig.OfflineTime.ToString();            ltrPageSize.Text = config.froumConfig.PageSize.ToString();            ltrReplyCount.Text = config.froumConfig.ReplyCount.ToString();            ltrRootUrl.Text = config.froumConfig.RootUrl.ToString();

以上就是.Net設定檔——統一節點組態管理的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.