自定义组件,轻松完成自定义节点配置及读取操作(通用组件)

在app.config和web.config中我们通常要用自定义节点来完成想要扩展的配置内容,以前我们需要继承ConfigurationSection,ConfigurationElementCollection,ConfigurationElement这些类来完成配置信息的读取代码比较繁琐复杂。后来有了linq to xml 通过它我们很容易查询出配置信息,但是这样却少了类的定义,只能通过XmlNode来读取或者填充我们定义的类。

一、原来自定配置文件的编写方式:

1、定义类型比较繁琐

internal class AOPConfigurationSection : ConfigurationSection { [ConfigurationProperty("", IsDefaultCollection = true)] public AopElementCollection Aops //需要定义这里略 { get { return (AopElementCollection)base[""]; } } }

2、LINQ TO XML查询

XElement xelement = XElement.Parse(xml); var name = from e in xelement.Elements("b") let s = e.Element("e") select s.Attribute("name").Value;



二、通用配置组件介绍

引用:DefinitionConfig.dll

对象:

DisplayConfigName特性(对应节点名称)

ReadSection类(初始化节点)

ConfigSectionsHelper类(配置解析类)

Sections类(配置集合类)

特点:根据自定义类型读取配置文件

注意:定义属性为string或class类型,集合为泛型Sections类

方法:GetConfigSection();//读取唯一节点类型

GetConfigSectionChild();//读取包含子节点类型

1.自定义类型

public class ConnConfig { [DisplayConfigName("name")]//配置文件中名称为name public string Name { get; set; } public string str{ get; set; }//如果没有声明特性,那么配置文件名称为属性名称str }

2.单节点(不含子节点)

//这里section 中type属性为DefinitionConfig.ConfigSectionsHelper,DefinitionConfig









db

connstring


上面我们只配置了一个connstr节点。没有任何子节点。注:此节点只能有一个,所以不能多个connstr。

我们把这个配置读取为ConnConfig类型。

ReadSection rs = new ReadSection("connstr");//实例化ReadSection,参数为节点名称

ConnConfig conn= rs.GetConfigSection();//通过GetConfigSection读取配置

Console.WriteLine(conn.Name);//验证是否读取到

3、多节点(含子节点)











connstring





sqlite

connstring





ReadSection rs = new ReadSection("connstrs");//读取connstrs节点 var con = rs.GetConfigSectionChild();//GetConfigSectionChild读取子节点配置,注:只要有子节点配置都需要用这个来读取 foreach (var item in con) { Console.WriteLine(item.Name); Console.WriteLine(item.str); }

4、属性为自定义类型(含多个子节点)

public class ConnConfig { [DisplayConfigName("name")] public string Name { get; set; } public ConnString str { get; set; }//定义为类型 }

public class ConnString { public string name { get; set; } public string type { get; set; } }

oracle oracle10



ReadSection rs = new ReadSection("connstrs"); var con = rs.GetConfigSectionChild(); Console.WriteLine(con[0].str.name);//oracledb

5、属性为自定义集合类型(子节点集合)

public class ConnConfig { [DisplayConfigName("name")] public string Name { get; set; } public ConnString str { get; set; } public Sections aa { get; set; }//定义集合 }

public class ConnString { public string name { get; set; } public string type { get; set; } }

public class AA{ public string name{get;set;} }

oracle oracle10 2



ReadSection rs = new ReadSection("connstrs"); var con = rs.GetConfigSectionChild(); foreach (var item in con[0].aa) { Console.WriteLine(item.name); }

6、属性为自定义多个集合类型(多子节点集合)

public class ConnConfig { [DisplayConfigName("name")] public string Name { get; set; } public ConnString str { get; set; } public Sections aa { get; set; } }

public class ConnString { public string name { get; set; } public string type { get; set; } }

public class AA { public string name { get; set; } public Sections bb { get; set; } }

public class BB { public string type { get; set; } }

oracle oracle10 2



ReadSection rs = new ReadSection("connstrs"); var con = rs.GetConfigSectionChild(); foreach (var item in con[0].aa) { Console.WriteLine(item.name); }

7、配置外部config





ReadSection rs = new ReadSection("mySection");//读取节点 var list = rs.GetConfigSectionChild();



组件下载:DefinitionConfig

组件Demo:ReadConfigDemo

Tags:  新浪自定义组件 博客自定义组件 自定义组件

延伸阅读

最新评论

发表评论