[转].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
[转].net自定义configSections的5个示例的更多相关文章
- C# 基于泛型的自定义线性节点链表集合示例
本例子实现了如何自定义线性节点集合,具体代码如下: using System; using System.Collections; using System.Collections.Generic; ...
- Spring MVC中自定义拦截器的简单示例
1. 引言 拦截器(Interceptor)实现对每一个请求处理前后进行相关的业务处理,类似于Servlet的Filter. 我们可以让普通的Bean实现HandlerIntercpetor接口或继承 ...
- Android自定义组合控件详细示例 (附完整源码)
在我们平时的Android开发中,有时候原生的控件无法满足我们的需求,或者经常用到几个控件组合在一起来使用.这个时候,我们就可以根据自己的需求创建自定义的控件了,一般通过继承View或其子类来实现. ...
- java 自定义注解,并使用示例
场景: 对需要校验 手机验证码和短信验证码的controller方法添加 自定义的注解 @CheckType 1. 定义注解 /** * 需要短信.验证码验证方法上的注解 * date: 2018年 ...
- 自定义JS控件-简单示例
1. 业务需求: 制作 一个按钮对象,然后 像 winfrom 那样调用 就可以了: 首先 我们新建一个 MyControls的 JS文件:(插入如下代码) //这里运用的面向对象的思想 ,新建了 ...
- PHP实现的自定义图像居中裁剪函数示例
图像居中裁减的大致思路: 1.首先将图像进行缩放,使得缩放后的图像能够恰好覆盖裁减区域.(imagecopyresampled ― 重采样拷贝部分图像并调整大小) 2.将缩放后的图像放置在裁减区域中间 ...
- 类库探源——System.Configuration 配置信息处理
按照MSDN描述 System.Configuration 命名空间 包含处理配置信息的类型 本篇文章主要两方面的内容 1. 如何使用ConfigurationManager 读取AppSetting ...
- CAS自定义登录验证方法
一.CAS登录认证原理 CAS认证流程如下图: CAS服务器的org.jasig.cas.authentication.AuthenticationManager负责基于提供的凭证信息进行用户认证.与 ...
- [039] 微信公众帐号开发教程第15篇-自定义菜单的view类型(访问网页)
引言及内容概要 距离写上一篇文章<自定义菜单的创建及菜单事件响应>整整过了两个月的时间,那时公众平台还没有开放view类型的菜单.在不久前,微信公众平台悄悄开放了view类型的菜单,却没有 ...
随机推荐
- apple配置WIFI热点
打开AirPort打开设置偏好-共享,找到WIFI相关
- Circle(codevs 3134)
题目描述 Description 在一个圆上,有2*K个不同的结点,我们以这些点为端点,连K条线段,使得每个结点都恰好用一次.在满足这些线段将圆分成最少部分的前提下,请计算有多少种连线的方法 输入描述 ...
- Cookie的使用与实现记住用户名案例
学习web开发,使用Cookie是不可避免的,个人感觉Cookie的使用和ASP.NET中的Session非常像,只不过Cookie是保存在客户端,而Session是在服务器端,两者都以记录信息为目的 ...
- hdu 2199:Can you solve this equation?(二分搜索)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HTML <img> 标签的 height 和 width 属性
定义和用法 <img> 标签的 height 和 width 属性设置图像的尺寸. 提示:为图像指定 height 和 width 属性是一个好习惯.如果设置了这些属性,就可以在页面加载时 ...
- C++primer学习笔记(二)——Chapter 4
4.1 Fundamentals 1.Basic Concepts (1)操作符分为一元,二元或者三元操作符: (2)复杂的表达式中含有很多操作符时: 规则一:分为不同的级别,级别高的先运行: 规则 ...
- SqlServer数据库字典--表.视图.函数.存储过程.触发器.主键.外键.约束.规则.sql
SELECT DISTINCT TOP 100 PERCENT isnull(p.name,'') AS 父对象, o.xtype, CASE o.xtype WHEN 'C' ...
- CodeForces 656B
C - C Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- org.springframework.beans包
beans包中最核心的两个类:DefaultListableBeanFactory&XmlBeanDefinitionReader DefaultListableBeanFactory Xml ...
- Python实践:模块自动重载
一.概述 二.思路 三.实现 四.测试 1.开启自动重载(终端1) 2.修改模块(终端2) 3.查看实时输出(终端1) 五.参考源码 一.概述 开发Web程序时,通常会采用本地服务器进行调试,但如果代 ...