[转].net自定义configSections的5个示例
<?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("\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 }
引用: .net自定义configSections的5个示例 http://www.yongfa365.com/item/configuration-configSections-SingleTagSectionHandler-DictionarySectionHandler-NameValueSectionHandler.html
posted on 2013-08-27 10:00 freeliver54 阅读(355) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现