ExampleConfigurationSectionHandler
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的更多相关文章
随机推荐
- P2426 删数
P2426 删数 题目描述 有N个不同的正整数数x1, x2, ... xN 排成一排,我们可以从左边或右边去掉连续的i(1≤i≤n)个数(只能从两边删除数),剩下N-i个数,再把剩下的数按以上操作处 ...
- 题解 P1967 【货车运输】
树链剖分+线段树 思路 貌似题解里没有树链剖分和线段树的,贡献一发. 首先明确题目要求:一辆车走某条路从x城到y城的边权最小值 我们把要求分开来看: 从x城到y城:我们需要走的路径将两点联通 边权最小 ...
- 手把手教你使用webpack搭建vue框架
我们在使用vue开发项目的时候,都是用vue-cli直接来搭建的.但是这是别人已经造好的轮子,我们既然要使用别人造好的轮子,我们总不能知其然而不知其所以然.所以呢,我这边文章就教你如何使用webpac ...
- Java实现各种内部排序算法
数据结构中常见的内部排序算法: 插入排序:直接插入排序.折半插入排序.希尔排序 交换排序:冒泡排序.快速排序 选择排序:简单选择排序.堆排序 归并排序.基数排序.计数排序 直接插入排序: 思想:每次将 ...
- Document对象中的一些重要的属性和方法(笔记)
Document对象:每一个web浏览器窗口.标签页和框架由一个window对象所表示.每个window对象都有一个document属性引用的是Document对象,它是一个巨大的API中的核心对象, ...
- ASP.NET 应用生命周期19个事件简介
下面是请求管道中的19个事件. (1)BeginRequest: 开始处理请求 (2)AuthenticateRequest授权验证请求,获取用户授权信息 (3):PostAuthenticateRe ...
- 小白欢乐多——记ssctf的几道题目
小白欢乐多--记ssctf的几道题目 二哥说过来自乌云,回归乌云.Web400来源于此,应当回归于此,有不足的地方欢迎指出. 0x00 Web200 先不急着提web400,让我们先来看看web200 ...
- IE安全系列之——RES Protocol
IE安全系列之--RES Protocol res Protocol用于从一个文件里面提取指定资源.语法为:res://sFile[/sType]/sID 各Token含义: sfile:百分号编码. ...
- dump函数
一.函数标准格式: DUMP(expr[,return_fmt[,start_position][,length]]) 基本参数时4个,最少可以填的参数是0个.当完全没有参数时,直接返回null.另外 ...
- springcloud中eureka集群unavailable-replicas
unavailable-replicas 配置了集群,但是在注册中心显示另外的几个集群地址是不可用的: 1 首先需要再host中添加服务名映射,如果应映射了再看是否在yml中配置了prefer-ip- ...