自定义配置节点configSections的使用
//App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!--添加自定义配置节点,type:解析类名,程序集名-->
<section name="PersonSetion" type="CommonConfig.PersonSectionHandler,CommonConfig"/>
</configSections>
<!--自定义节点内容-->
<PersonSetion>
<PersonInfo name="Name" Value="Mr Lin" ReadOnly="true"></PersonInfo>
<PersonInfo name="Department" Value="Development" ReadOnly="true"></PersonInfo>
<PersonInfo name="Position" Value="Software Engineer" ReadOnly="true"></PersonInfo>
</PersonSetion>
</configuration>
//解析自定义节点
using System;
using System.Configuration;
using System.Xml;
using Model;
namespace CommonConfig
{
/// <summary>
/// 实现接口:IConfigurationSectionHandler,解析自定义配置节点,
/// </summary>
public class PersonSectionHandler : IConfigurationSectionHandler
{
public object Create(object parent, object configContext, XmlNode section)
{
//解析配置文件信息,返回对象
Person person = new Person();
if (section != null)
foreach (XmlNode item in section.SelectNodes("PersonInfo"))
{
switch (item.Attributes["name"].InnerText)
{
case "Name":
person.Name = item.Attributes["Value"].InnerText;
person.IsNameReadOnly = Convert.ToBoolean(item.Attributes["ReadOnly"].InnerText);
break;
case "Department":
person.Department = item.Attributes["Value"].InnerText;
person.IsDepartmentReadOnly = Convert.ToBoolean(item.Attributes["ReadOnly"].InnerText);
break;
case "Position":
person.Position = item.Attributes["Value"].InnerText;
person.IsPositionReadOnly = Convert.ToBoolean(item.Attributes["ReadOnly"].InnerText);
break;
default:
break;
}
}
return person;
}
}
}
//实体类
namespace Model
{
public class Person
{
private string name;
private string department;
private string position;
private bool isNameReadOnly;
private bool isDepartmentReadOnly;
private bool isPositionReadOnly;
/// <summary>
/// 姓名
/// </summary>
public string Name
{
get { return name; }
set { name = value; }
}
/// <summary>
/// 部门
/// </summary>
public string Department
{
get { return department; }
set { department = value; }
}
/// <summary>
/// 职位
/// </summary>
public string Position
{
get { return position; }
set { position = value; }
}
/// <summary>
/// 名称是否只读
/// </summary>
public bool IsNameReadOnly
{
get { return isNameReadOnly; }
set { isNameReadOnly = value; }
}
/// <summary>
/// 部门信息是否只读
/// </summary>
public bool IsDepartmentReadOnly
{
get { return isDepartmentReadOnly; }
set { isDepartmentReadOnly = value; }
}
/// <summary>
/// 职位信息是否只读
/// </summary>
public bool IsPositionReadOnly
{
get { return isPositionReadOnly; }
set { isPositionReadOnly = value; }
}
}
}
//测试配置
using System;
using System.Configuration;
using System.Windows.Forms;
using Model;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SetText();
}
private void SetText()
{
//会调用object Create(object parent, object configContext, XmlNode section)
Person person= (Person)ConfigurationSettings.GetConfig("PersonSetion");
if (person != null)
{
txtDepartment.Text = person.Department;
txtDepartment.ReadOnly = person.IsDepartmentReadOnly;
txtName.Text = person.Name;
txtName.ReadOnly = person.IsNameReadOnly;
txtPosition.Text = person.Position;
txtPosition.ReadOnly = person.IsPositionReadOnly;
}
}
}
}
转转:http://blog.sina.com.cn/s/blog_5b9b514b0100p5gq.html
自定义配置节点configSections的使用的更多相关文章
- VS2012 常用web.config配置解析之自定义配置节点
在web.config文件中拥有一个用户自定义配置节点configSections,这个节点可以方便用户在web.config中随意的添加配置节点,让程序更加灵活(主要用于第三方插件的配置使用) 自定 ...
- App.config和Web.config配置文件的自定义配置节点
前言 昨天修改代码发现了一个问题,由于自己要在WCF服务接口中添加了一个方法,那么在相应调用的地方进行更新服务就可以了,不料意外发生了,竟然无法更新.左查右查终于发现了问题.App.config配置文 ...
- ASP.NET系列:自定义配置节点的复用
appSettings太简单,为每个程序自定义配置节点太复杂,因此要解决app.config&web.config自定义配置的复用问题. 1.读取不依赖SectionName,根节点可以定义为 ...
- .Net 配置文件--继承ConfigurationSection实现自定义处理类处理自定义配置节点
除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...
- C#创建自定义配置节点
转载:http://www.educity.cn/develop/495003.html 在.Net应用程序中我们经常看到VS为我们生成的项目工程中都会含有app.config或者web.connfi ...
- App.Config自定义配置节点
配置文件: <?xml version="1.0" encoding="utf-8"?> <configuration> <con ...
- C# 快捷使用自定义配置节点
C#除了appSettings和connectionStrings默认配置外还允许用户自定义使用配置.C# 提供3中简单的自定义配置,配置文件如下 <?xml version="1.0 ...
- C# 创建自定义配置节点1
转载:http://www.educity.cn/develop/495003.html 在.Net应用程序中我们经常看到VS为我们生成的项目工程中都会含有app.config或者web.connfi ...
- .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点
除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...
随机推荐
- spring-cloud学习BUG
1.com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: c ...
- 一个显示 OpenCV Mat 图像的自定义 Qt 控件
今天学习 Qt 的时候顺手写了一个,包含一个头文件 qcvdisplay.h 和一个源文件 qcvdisplay.cpp,因为这是 qt 默认的文件命名方式,在 Qt Designer 中提升控件时会 ...
- 深入理解JAVA虚拟机 垃圾收集器和内存分配策略
引用计数算法 很多教科书判断对象是否存活的算法是这样的:给对象中添加一个引用计数器,每当有一个地方引用它时,计数器值就加1:当引用失效时,计数器值就减1:任何时刻计数器都为0的对象就是不可能再被使用的 ...
- vue 解决axios 跨域问题
闲着没事,假期敲vue 请求数据,总结下vue跨越问题 第一种.服务器服务器不支持跨域请求 1.当跨域无法请求的时候我们可以修改工程下config文件夹下的index.js中的dev:{}部分. ...
- python基础31[常用模块介绍]
python基础31[常用模块介绍] python除了关键字(keywords)和内置的类型和函数(builtins),更多的功能是通过libraries(即modules)来提供的. 常用的li ...
- day_04 基本数据类型的结构和使用方法
1. 简述Python的五大数据类型的作用.定义方式.使用方法: 1). 数字类型: 整型(int): 表示年龄.号码.级别:变量名=值,变量名=int(值):加减乘除.逻辑判断: 浮点型(float ...
- iOS给UIView添加点击事件
我要给一个UIView对象topView添加点击事件,方法如下: 步骤1:创建手势响应函数 (void)event:(UITapGestureRecognizer *)gesture { //处理事件 ...
- Docker(五):Dockerfile
我们使用 Dockerfile 定义镜像,依赖镜像来运行容器,因此 Dockerfile 是镜像和容器的关键,Dockerfile 可以非常容易的定义镜像内容,同时在我们后期的微服务实践中,Docke ...
- Jenkins自动打包并部署(以java -jar形势运行)
1.打包 与平常maven项目打包一致,不再赘述 2.杀死原有进程 通过 pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' ` 获取当前 ...
- springboot+mybatis日志显示SQL的最简单方法
在springBoot+Mybatis日志显示SQL的执行情况的最简单方法就是在properties新增:logging.level.cn.piesat.mapper=debug 注意:其中cn.pi ...