zoukankan      html  css  js  c++  java
  • [转].net自定义configSections的5个示例

    本文转自:http://www.yongfa365.com/item/configuration-configSections-SingleTagSectionHandler-DictionarySectionHandler-NameValueSectionHandler.html

    <?xml version="1.0"?>
    <configuration>
    
      <configSections>
        <!--最简的三种,使用系统Handler,不用写C#代码配置,直接调用-->
        <section name="SingleTagSectionHandler" type="System.Configuration.SingleTagSectionHandler"/>
        <section name="DictionarySectionHandler" type="System.Configuration.DictionarySectionHandler"/>
        <section name="NameValueSectionHandler" type="System.Configuration.NameValueSectionHandler"/>
    
        <!--自定义section,需要预先在程序里定义-->
        <section name="MyBlogSection" type="AboutCustomConfiguration.MyBlogSection,AboutCustomConfiguration"/>
        <section name="MySiteSection" type="AboutCustomConfiguration.MySiteSection,AboutCustomConfiguration"/>
      </configSections>
    
      <SingleTagSectionHandler yongfa365="http://www.yongfa365.com/" cnblogs="http://www.cnblogs.com/"/>
    
      <DictionarySectionHandler>
        <add key="yongfa365" value="http://www.yongfa365.com/"/>
        <add key="cnblogs" value="http://www.cnblogs.com/"/>
      </DictionarySectionHandler>
    
      <NameValueSectionHandler>
        <add key="yongfa365" value="http://www.yongfa365.com/"/>
        <add key="cnblogs" value="http://www.cnblogs.com/"/>
      </NameValueSectionHandler>
    
      <MyBlogSection>
        <blogs>
          <add UserName="yongfa365" BlogUrl="http://www.yongfa365.com/" Hits="12345" />
          <add UserName="cnblogs" BlogUrl="http://www.cnblogs.com/" Hits="54321" />
        </blogs>
      </MyBlogSection>
    
    
      <MySiteSection>
        <yongfa365 UserName="yongfa365" BlogUrl="http://www.yongfa365.com/" Hits="12345" />
        <cnblogs UserName="cnblogs" BlogUrl="http://www.cnblogs.com/" Hits="54321" />
      </MySiteSection>
    
    
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
      </startup>
    </configuration>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    using System.Collections;
    using System.Collections.Specialized;
    
    namespace AboutCustomConfiguration
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("
    SingleTagSectionHandler:");
                var dicSingleTagSectionHandler = ConfigurationManager.GetSection("SingleTagSectionHandler") as IDictionary;
                foreach (DictionaryEntry item in dicSingleTagSectionHandler)
                {
                    Console.WriteLine("Key:{0} Value:{1}", item.Key, item.Value);
                }
    
    
                Console.WriteLine("
    DictionarySectionHandler:");
                var dictDictionarySectionHandler = ConfigurationManager.GetSection("DictionarySectionHandler") as IDictionary;
                foreach (string key in dictDictionarySectionHandler.Keys)
                {
                    Console.WriteLine("Key:{0} Value:{1}", key, dictDictionarySectionHandler[key]);
                }
    
    
    
                Console.WriteLine("
    NameValueSectionHandler:");
                var dictNameValueCollection = ConfigurationManager.GetSection("NameValueSectionHandler") as NameValueCollection;
                foreach (string key in dictNameValueCollection.Keys)
                {
                    Console.WriteLine("Key:{0} Value:{1}", key, dictNameValueCollection[key]);
                }
    
    
    
    
                Console.WriteLine("
    MyBlogSection:");
                var myBlogSection = ConfigurationManager.GetSection("MyBlogSection") as MyBlogSection;
                foreach (Blog item in myBlogSection.Blogs)
                {
                    Console.WriteLine("Key:{0} Value:{1}", item.UserName, item.BlogUrl);
                }
    
                Console.WriteLine("
    MySiteSection:");
                var mySiteSection = ConfigurationManager.GetSection("MySiteSection") as MySiteSection;
                Console.WriteLine(mySiteSection.CnBlogs.BlogUrl);
                Console.WriteLine(mySiteSection.YongFa365.BlogUrl);
    
    
            }
        }
    
        #region MyBlogSection
    
        public class MyBlogSection : ConfigurationSection
        {
            [ConfigurationProperty("blogs", IsDefaultCollection = false)]
            public Blogs Blogs { get { return (Blogs)base["blogs"]; } }
        }
    
        //[ConfigurationCollection(typeof(Blogs),AddItemName="add")]
        public class Blogs : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new Blog();
            }
            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((Blog)element).UserName;
            }
        }
    
        public class Blog : ConfigurationElement
        {
            #region 配置節設置,設定檔中有不能識別的元素、屬性時,使其不報錯
            ///         /// 遇到未知屬性時,不報錯         ///
            ///
            ///
            ///
            protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
            {
                //return base.OnDeserializeUnrecognizedAttribute(name, value);
                return true;
            }
    
            ///         /// 遇到未知元素時,不報錯         ///
            ///
            ///
            /// 
            protected override bool OnDeserializeUnrecognizedElement(string elementName, System.Xml.XmlReader reader)
            {
                //return base.OnDeserializeUnrecognizedElement(elementName, reader);
                return true;
            }
            #endregion
    
            [ConfigurationProperty("UserName", IsRequired = true)]
            public string UserName { get { return this["UserName"].ToString(); } }
    
            [ConfigurationProperty("BlogUrl", IsRequired = true)]
            public string BlogUrl { get { return this["BlogUrl"].ToString(); } }
    
            [ConfigurationProperty("Hits", IsRequired = true)]
            public int Hits { get { return (int)this["Hits"]; } }
    
        }
        #endregion
    
    
        #region MySiteSection
    
        public class MySiteSection : ConfigurationSection
        {
            [ConfigurationProperty("cnblogs", IsDefaultCollection = false)]
            public Blog CnBlogs { get { return (Blog)base["cnblogs"]; } }
    
            [ConfigurationProperty("yongfa365", IsDefaultCollection = false)]
            public Blog YongFa365 { get { return (Blog)base["yongfa365"]; } }
        }
    
    
        #endregion
    
    
    
    
    }
    

    引用: .net自定义configSections的5个示例 http://www.yongfa365.com/item/configuration-configSections-SingleTagSectionHandler-DictionarySectionHandler-NameValueSectionHandler.html

  • 相关阅读:
    【PHP】php重写session的存储机制
    【Javascript】原生js 全特效微博发布面板效果实现
    【Javascript】原生js百叶窗效果的实现及原理介绍
    【jQuery】jquery全屏滚动插件【fullPage.js】API 使用方法总结
    从百度音乐和酷狗音乐的分类想到的
    jQuer __Ajax DOM
    面向对象闭包 继承
    Git
    设计模式
    html5标签大全
  • 原文地址:https://www.cnblogs.com/freeliver54/p/3284293.html
Copyright © 2011-2022 走看看