[转].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类型的菜单,却没有 ...
随机推荐
- 【HTTP协议】响应头中的Content-Length和Transfer-Encoding
来源: http://blog.csdn.net/superhosts/article/details/8737434 http://bbs.csdn.net/topics/390384017 对于h ...
- ACdream 1195 Sudoku Checker (数独)
Sudoku Checker Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Submit ...
- Light OJ 1199 - Partitioning Game (博弈sg函数)
D - Partitioning Game Time Limit:4000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- Zookeeper笔记(四)Zookeeper在Dubbo中的应用
Zookeeper在Dubbo中的应用 Dubbo的架构 节点角色说明: Provider: 暴露服务的服务提供方.Consumer: 调用远程服务的服务消费方.Registry: 服务注册与发现的注 ...
- sdut 2411:Pixel density(第三届山东省省赛原题,字符串处理)
Pixel density Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Pixels per inch (PPI) or pi ...
- ytu 1304:串的简单处理(水题)
串的简单处理 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 39 Solved: 11[Submit][Status][Web Board] Desc ...
- js 默认行为取消
js 默认行为取消 可以简单的 return false; 看需求吧 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transit ...
- ML 03、机器学习的三要素
机器学习算法原理.实现与实践——机器学习的三要素 1 模型 在监督学习中,模型就是所要学习的条件概率分布或决策函数.模型的假设空间包含所有可能的条件概率分布或决策函数.例如,假设决策函数是输入变量的线 ...
- OD 内存映射 属主找不到当前程序名解决办法 和 跟随ClassProc 反汇编窗口空白解决办法
OD 内存映射 属主找不到当前程序名解决办法 取消 StrongOD 选项里 高级枚举模块选项就OK了 重启OD 跟随ClassProc 反汇编窗口空白解决办法 StrongOD.dll 是有问 ...
- 如何扫描出Android系统媒体库中视频文件
Android系统启动时会去扫描系统文件,并将系统支持的视频文件(mp4,3gp,wmv)扫描到媒体库(MediaStore)中,下面代码演示如何获得这些文件的信息: publicstatic Lis ...