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的更多相关文章
随机推荐
- 《剑指offer》 面试题53 :正则表达式匹配 Java
引言:这道题情况比较复杂,边界条件较多,为了便于以后复习,整理一下.另外,由于C语言和Java对于字符串的操作存在不一样的地方,代码也存在改动. 题目:请实现一个函数用来匹配包含'.'和'*'的正则表 ...
- jQuery中使用attribute,prop获取,设置input的checked值
1.prop方法获取.设置checked属性 当input控件checkbox设置了checked属性时,无论checked=”“或 checked=”checked”,$(obj).prop(“ch ...
- linux服务器上没有jar命令
在linux服务器上用jar命令解压jar包时,提示找不到jar命令. 但是用java -version查看jdk版本,又可以显示出jdk版本. echo $JAVA_HOME查看环境变量路径,找不到 ...
- bzoj千题计划139:bzoj2229: [Zjoi2011]最小割
http://www.lydsy.com/JudgeOnline/problem.php?id=2229 最小割树介绍:http://blog.csdn.net/jyxjyx27/article/de ...
- 2017 清北济南考前刷题Day 1 afternoon
期望得分:80+30+70=180 实际得分:10+30+70=110 T1 水题(water) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK出了道水 ...
- 何凯文每日一句打卡||DAY1~DAY3
01长难句 In an open meeting with congressional Democrats and Republicans, Trump embraced raising the ag ...
- c# 判断一个数是不是质数或者求一个数的公约数的算法
一个数是不是质数,就是判断一个数除了1和它本身还有没有其他的约数,如果有则是合数,否则是质数.其实本质都是求公约数. 求公约数是什么思路呢,就是找比它小的数不断尝试,能被整除则是其约数,否则继续尝试, ...
- a标签伪元素选择器
a{ color: black; } /*未访问的链接*/ a:link{ color: red; } /*访问过的链接*/ a:visited{ color: green; } /*鼠标经过时*/ ...
- 解析XML文件的几种常见操作方法:DOM/SAX/DOM4j
<?xml version="1.0" encoding="utf-8"?> <root> <class name="c ...
- 给Ubuntu替换阿里的源
1. 阿里巴巴镜像源站点 有所有linux的源的镜像加速. 点击查看介绍 2. 具体配置方法在这里 copy: ubuntu 18.04(bionic) 配置如下 创建自己的配置文件,比如创建文件 / ...