博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转].net自定义configSections的5个示例
阅读量:5366 次
发布时间:2019-06-15

本文共 4537 字,大约阅读时间需要 15 分钟。

本文转自:

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("\r\nSingleTagSectionHandler:");            var dicSingleTagSectionHandler = ConfigurationManager.GetSection("SingleTagSectionHandler") as IDictionary;            foreach (DictionaryEntry item in dicSingleTagSectionHandler)            {                Console.WriteLine("Key:{0} Value:{1}", item.Key, item.Value);            }            Console.WriteLine("\r\nDictionarySectionHandler:");            var dictDictionarySectionHandler = ConfigurationManager.GetSection("DictionarySectionHandler") as IDictionary;            foreach (string key in dictDictionarySectionHandler.Keys)            {                Console.WriteLine("Key:{0} Value:{1}", key, dictDictionarySectionHandler[key]);            }            Console.WriteLine("\r\nNameValueSectionHandler:");            var dictNameValueCollection = ConfigurationManager.GetSection("NameValueSectionHandler") as NameValueCollection;            foreach (string key in dictNameValueCollection.Keys)            {                Console.WriteLine("Key:{0} Value:{1}", key, dictNameValueCollection[key]);            }            Console.WriteLine("\r\nMyBlogSection:");            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("\r\nMySiteSection:");            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}

 

引用:

 

转载于:https://www.cnblogs.com/freeliver54/p/3284293.html

你可能感兴趣的文章
Saiku资源帖
查看>>
解决手机页面中点击文本框,网页放大问题
查看>>
2-5
查看>>
牛客多校3 A-PACM Team(状压降维+路径背包)
查看>>
HDU - 4284 Travel(floyd+状压dp)
查看>>
1027 制作表格
查看>>
Android之Socket通信、List加载更多、Spinner下拉列表
查看>>
面向对象的介绍与特性
查看>>
typing-python用于类型注解的库
查看>>
20189215 2018-2019-2 《密码与安全新技术专题》第13周作业
查看>>
第四周作业
查看>>
一、HTML基础
查看>>
蓝牙进阶之路 (002) - HC-05与HC-06的AT指令的区别(转)
查看>>
mysql的limit经典用法及优化
查看>>
C#后台程序与HTML页面中JS方法互调
查看>>
mysql 同一个表中 字段a 的值赋值到字段b
查看>>
linux系统可执行文件添加环境变量使其跨终端和目录执行
查看>>
antiSMASH数据库:微生物次生代谢物合成基因组簇查询和预测
查看>>
UNICODE与ANSI的区别
查看>>
nginx 配置实例
查看>>