.Net配置文件——统一节点配置管理

黄舟
Lepaskan: 2017-02-24 10:20:09
asal
1664 orang telah melayarinya

       对于.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>
Salin selepas log masuk

处理类:

 /// <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;
        }
    }
Salin selepas log masuk

上面的类主要是为了实现创建具体配置节点管理类,在配置节点的管理类中,存有各个子节点类的引用。

 /// <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);

            }
        }
    }
Salin selepas log masuk

这样,我们使用的时候,每次都用这一个类来实现对节点的访问:

 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();
Salin selepas log masuk

 以上就是.Net配置文件——统一节点配置管理的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan