自定义配置节点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配置文件中一个 ...
随机推荐
- JS Unicode转中文,中文转Unicode,ASCII转Unicode,Unicode转ASCII
在线转换工具https://oktools.net/unicode Unicode转中文 function decodeUnicode(str) { return unescape(str.repla ...
- intellij 编译 springmvc+hibernate+spring+maven 找不到hbm.xml映射文件
1. 错误信息 Invocation of init method failed; nested exception is org.hibernate.MappingNotFoundException ...
- Hash介绍
Hash,一般翻译做"散列",也有直接音译为"哈希"的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就 ...
- mybatis 动态SQL查询总结
背景 ××项目需要提供系统部分函数第三方调用接口,基于安全性和避免暴露数据库表信息的基础上进行函数接口的设计,根据第三方调用身份的权限提供某张表的自定义集合.本项目基于mybatis的持久层框架,支持 ...
- 关于android studio从2.3升级到3.0以上可能会遇到的问题
请参考链接: http://blog.csdn.net/hylczp/article/details/60137958 gradle-3.3-all网盘下载地址: 链接:http://pan.baid ...
- SpringBootMybatis 关于Mybatis-generator-gui的使用|数据库的编码注意点|各项复制模板
mysql注意点: .有关编码 create table user( id int primary key auto_increment, `name` varchar(), `password` v ...
- php正则替换非站内链接 替换zencart描述内的非本站链接
php正则替换非站内链接 <?php //要替换的文本,比如产品描述中的文字 header("content-Type: text/html; charset=utf-8") ...
- cve-2019-1609,Harbor任意管理员注册漏洞复现
一.Harbor介绍 以Docker为代表的容器技术的出现,改变了传统的交付方式.通过把业务及其依赖的环境打包进Docker镜像,解决了开发环境和生产环境的差异问题,提升了业务交付的效率.如何高效地管 ...
- java选做猜数字
程序设计思想 第一步:使用随机数生成1-100的数字 第二步:让用户输入数字 第三步:输入的数字与生成数字不同执行下一步,相同执行第五步 第四步:比较两数大小并输出结果,并返回第二步 第五步:输出猜对 ...
- ZROI 19.07.30 简单字符串/ll
写在前面:今天下午药丸--不会字符串,全程掉线/ll 给出字符串\(S\),\(q\)次询问,每次给出\(a,b,c,d\),询问\(S[a,b]\)的所有子串和\(S[c,d]\)最长公共前缀的最大 ...