转载:http://www.educity.cn/develop/495003.html
在.
可是有时候
添加一些自定义的元素到配置文件中是很容易的
1、
1: <?xml version="1.0" encoding="utf-8" ?>
2: <configuration>
3: <configSections>
4: <section name="dbFactory"
5: type ="DbFactory.Configuration.DbFactorySection,DbFactory.Configuration" />
6: </configSections>
7: </configuration>
2、 在适当的位置添加自定义的节点代码如下:
1: <?xml version="1.0" encoding="utf-8" ?>
2: <configuration>
3: <configSections>
4: <section name="dbFactory"
5: type ="DbFactory.Configuration.DbFactorySection,DbFactory.Configuration" />
6: </configSections>
7:
8: <dbFactory >
9: <default factory="sql"></default>
10: <factorys>
11: <add name="sql" assembly="HelloData" class="SqlDbFactory" />
12: <add name="oracle" assembly="HelloData" class="OracleDbFactory" />
13: </factorys>
14: </dbFactory>
15: </configuration>
自定义节点算是添加完了,
ConfigurationSection:
ConfigurationElement:
ConfigurationElementCollection:
有了这些类
第一:
第二:
概念往往都比较抽象,
在<dbFactory> 配置节中有两个元素,
<default>元素
代码
1: public class DefaultElement : ConfigurationElement
2: {
3: [ConfigurationProperty("factory")]
4: public string Factory
5: {
6: get { return this["factory"] as string; }
7: set { this["factory"] = value; }
8: }
9: }
注意在属性定义上面我们需要注册该属性的ConfigurationProperty特性。
<factorys>子元素
代码:
1: public class FactoryElement : ConfigurationElement
2: {
3: [ConfigurationProperty("name")]
4: public string Name
5: {
6: get
7: {
8: return this["name"] as string;
9: }
10: set
11: {
12: this["name"] = value;
13: }
14: }
15: [ConfigurationProperty("assembly")]
16: public string Assembly
17: {
18: get
19: {
20: return this["assembly"] as string;
21: }
22: set
23: {
24: this["assembly"] = value;
25: }
26: }
27: [ConfigurationProperty("class")]
28: public string Class
29: {
30: get
31: {
32: return this["class"] as string;
33: }
34: set
35: {
36: this["class"] = value;
37: }
38: }
39: }
<factorys>元素是集合型元素继承ConfigurationElementCollection
代码:
1: public class FactoryElements : ConfigurationElementCollection
2: {
3: protected override ConfigurationElement CreateNewElement()
4: {
5: return new FactoryElement();
6: }
7: protected override object GetElementKey(ConfigurationElement element)
8: {
9: return ((FactoryElement)element).Name;
10: }
11: public FactoryElement this[string name]
12: {
13: get
14: {
15: return BaseGet(name) as FactoryElement;
16: }
17: }
18: }
ConfigurationElementCollection类是个抽象类,
<dbFactory>节点
代码
1: public class DbFactorySection : ConfigurationSection
2: {
3: [ConfigurationProperty("default")]
4: public DefaultElement DefaultFactory
5: {
6: get { return this["default"] as DefaultElement; }
7: set { this["default"] = value; }
8: }
9: [ConfigurationProperty("factorys")]
10:
11: public FactoryElements Factorys
12: {
13: get
14: {
15: return this["factorys"] as FactoryElements;
16: }
17: set
18: {
19: this["factorys"] = value;
20: }
21: }
22: }
配置节处理程序终于写完了。把这四个类放在同一个工程目录下, 编译成一个DLL, 在你需要获取配置信息的地方 引用这个DLL, 用DbFactorySection section = ConfigurationManager. GetSection( “ dbFactory” ) as DbFactorySection;试试 section是不是你想要的东西呢?