ExampleConfigurationSectionHandler.cs

 using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization; namespace SampleConfigSectionHandle
{
public sealed class ExampleConfigurationSectionHandler : IConfigurationSectionHandler
{
public object Create(object parent, object configContext, XmlNode section)
{
var xmlSerializer = new XmlSerializer(typeof(UserInfo)); using (var stream = new MemoryStream(Encoding.Default.GetBytes(section.InnerXml)))
{
var xmlNode = XmlReader.Create(stream); return xmlSerializer.Deserialize(xmlNode);
}
}
} public class UserInfo
{
public string UserName { get; set; } public string UserPwd { get; set; }
}
}

App.config

 <?xml version="1.0" encoding="utf-8" ?>
<configuration> <configSections> <!--示例配置节-->
<section name="example" type="SampleConfigSectionHandle.ExampleConfigurationSectionHandler,SampleConfigSectionHandle"/> </configSections> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup> <!--配置节实例-->
<example> <UserInfo>
<UserName>WangYa</UserName>
<UserPwd>123456</UserPwd>
</UserInfo> </example> </configuration>

Program.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace SampleConfigSectionHandle
{
class Program
{
static void Main(string[] args)
{
var config = ConfigurationManager.GetSection("example") as UserInfo; if (config != null)
{
Console.WriteLine(config.UserName);
Console.WriteLine(config.UserPwd);
} Console.Read();
}
}
}

ExampleConfigurationSectionHandler的更多相关文章

随机推荐

  1. C++持有Object-C对象时容易内存泄露

    在IOS项目中,可以将C++与Object-C混编,不过必须放在实现文件.mm中. 在.mm中,我们可能创建了一个C++对象A,而它持有一个Object-C对象B作为成员变量.当A对象被释放掉的时候, ...

  2. Ubuntu14.04 Tab键自动补全

    Unbuntu14.04 终端中使用Tab键不能自动补全 解决方案 1.利用vi编辑器打开 /etc/bash.bashrc文件(需要root权限) sudo vi /etc/bash.bashrc ...

  3. ZeroMQ API(五) 传输模式

    1.使用TCP的单播传输:zmq_tcp(7) 1.1 名称 zmq_tcp - 使用TCP的ZMQ单播传输 1.2 概要 TCP是一种无处不在,可靠的单播传输.当通过具有ZMQ的网络连接分布式应用程 ...

  4. js生成接口请求参数签名加密

    js生成接口请求参数签名加密 定义规则:将所有参数字段按首字母排序, 拼接成key1 = value1 & key2 = value2的格式,再在末尾拼接上key = appSecret, 再 ...

  5. HDU 4707 Pet 邻接表实现

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707 解题报告:题目大意是在无向图G中有n个点,分别从0 到n-1编号,然后在这些点之间有n-1条边, ...

  6. JavaScript的基本介绍

    JavaScript入门介绍 输出语句:document.write()   1.执行顺序:从上到下,每一天语句是要加分号的,如果不加的话,浏览器会默认帮你自动添加,分号.   2.注释:一行注释就是 ...

  7. 33、Map简介

    Map接口概述 除了Collection之外,常用的集合还有Map接口,里面常用的实现类图如下: map中的元素是以键-值的方式存在的,通过键可以获取到值,键是不可以重复的,跟地图比较像,通过一个坐标 ...

  8. Linux基础-编译安装Python

    终于涉及一点儿专业的了,说实话,对于目前的我难度还是挺大的,这句话送给未来的自己 挑战开始: 首先了解一下Python3.6,底层是由c++开发的,所以在linux下需要C++的支持,必然少不了gcc ...

  9. Postman和Selenium IDE开局自带红蓝BUFF属性,就问你要还是不要

    话不多说,下面给大家介绍两款工具,selenium IDE和Postman. 为什么说是自带红蓝Buff,因为想做UI自动化和接口自动化的同学,很多时候,都难在了开头. 比如你要学习语言,你要学习框架 ...

  10. springboot集成mybatis环境搭建以及实现快速开发微服务商品模块基本的增删改查!

    之前学习了springboot和mybatis3的一些新特性,初步体会了springboot的强大(真的好快,,,,,),最近趁着复习,参考着以前学习的教程,动手写了一个springboot实战的小例 ...