自定义Net Configuration 类 - 继承ConfigurationSection
配置文件在应用程序开发中发挥及其重要的作用,使用配置文件可以动态改变应用程序的程序处理逻辑。
在.net framework 2.0平台中更是使得处理配置文件更加方便、灵活。同时微软也提供了自定义配置文件处理类的功能,
实现自定义配置类主要通过继承ConfigurationSection或者实现IConfigurationSectionHandler
这篇文章介绍的方法是继承 ConfigurationSection的 MySampleConfigurationSection
测试的app.config 文件
测试该Configuration代码
在.net framework 2.0平台中更是使得处理配置文件更加方便、灵活。同时微软也提供了自定义配置文件处理类的功能,
实现自定义配置类主要通过继承ConfigurationSection或者实现IConfigurationSectionHandler
这篇文章介绍的方法是继承 ConfigurationSection的 MySampleConfigurationSection
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace ConfigSample
{
public class MySampleConfigurationSection : ConfigurationSection
{
public MySampleConfigurationSection() { }
public MySampleConfigurationSection(string attr1)
{
MyAttribute1 = attr1;
}
[ConfigurationProperty("myAttribute1",DefaultValue="MyAttribute1-default-value",IsRequired=true)]
public string MyAttribute1
{
get { return (string)this["myAttribute1"]; }
set { this["myAttribute1"] = value; }
}
[ConfigurationProperty("myChildSection")]
public MyChildConfigurationElement MyAttributeElement
{
get { return (MyChildConfigurationElement)this["myChildSection"]; }
set { this["myChildSection"] = value; }
}
}
public class MyChildConfigurationElement : ConfigurationElement
{
public MyChildConfigurationElement() { }
public MyChildConfigurationElement(string attr1, string attr2)
{
MyChildAttribute1 = attr1;
MyChildAttribute2 = attr2;
}
[ConfigurationProperty("myChildAttribute1")]
public string MyChildAttribute1
{
get { return (string)this["myChildAttribute1"]; }
set { this["myChildAttribute1"] = value; }
}
[ConfigurationProperty("myChildAttribute2")]
public string MyChildAttribute2
{
get { return (string)this["myChildAttribute2"]; }
set { this["myChildAttribute2"] = value; }
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace ConfigSample
{
public class MySampleConfigurationSection : ConfigurationSection
{
public MySampleConfigurationSection() { }
public MySampleConfigurationSection(string attr1)
{
MyAttribute1 = attr1;
}
[ConfigurationProperty("myAttribute1",DefaultValue="MyAttribute1-default-value",IsRequired=true)]
public string MyAttribute1
{
get { return (string)this["myAttribute1"]; }
set { this["myAttribute1"] = value; }
}
[ConfigurationProperty("myChildSection")]
public MyChildConfigurationElement MyAttributeElement
{
get { return (MyChildConfigurationElement)this["myChildSection"]; }
set { this["myChildSection"] = value; }
}
}
public class MyChildConfigurationElement : ConfigurationElement
{
public MyChildConfigurationElement() { }
public MyChildConfigurationElement(string attr1, string attr2)
{
MyChildAttribute1 = attr1;
MyChildAttribute2 = attr2;
}
[ConfigurationProperty("myChildAttribute1")]
public string MyChildAttribute1
{
get { return (string)this["myChildAttribute1"]; }
set { this["myChildAttribute1"] = value; }
}
[ConfigurationProperty("myChildAttribute2")]
public string MyChildAttribute2
{
get { return (string)this["myChildAttribute2"]; }
set { this["myChildAttribute2"] = value; }
}
}
}
测试的app.config 文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="mySampleConfigurationSectionGroup">
<section name="mySampleConfigurationSection" type="ConfigSample.MySampleConfigurationSection,ConfigSample,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere"/>
</sectionGroup>
</configSections>
<mySampleConfigurationSectionGroup>
<mySampleConfigurationSection myAttribute1="MyAttribute1 value">
<myChildSection myChildAttribute1="MyChildAttribute1 value" myChildAttribute2="MyChildAttribute2 value" />
</mySampleConfigurationSection>
</mySampleConfigurationSectionGroup>
</configuration>
<configuration>
<configSections>
<sectionGroup name="mySampleConfigurationSectionGroup">
<section name="mySampleConfigurationSection" type="ConfigSample.MySampleConfigurationSection,ConfigSample,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere"/>
</sectionGroup>
</configSections>
<mySampleConfigurationSectionGroup>
<mySampleConfigurationSection myAttribute1="MyAttribute1 value">
<myChildSection myChildAttribute1="MyChildAttribute1 value" myChildAttribute2="MyChildAttribute2 value" />
</mySampleConfigurationSection>
</mySampleConfigurationSectionGroup>
</configuration>
测试该Configuration代码
// get sample configuration section
MySampleConfigurationSection myConfigSection = (MySampleConfigurationSection)ConfigurationManager.GetSection("mySampleConfigurationSectionGroup/mySampleConfigurationSection");
if (myConfigSection != null)
{
txtMyAttribute1.Text = myConfigSection.MyAttribute1.ToString();
txtMyChildAttribute1.Text = myConfigSection.MyAttributeElement.MyChildAttribute1.ToString();
txtMyChildAttribute2.Text = myConfigSection.MyAttributeElement.MyChildAttribute2.ToString();
}
else
{
MessageBox.Show("Cannot read the sample configuration value");
}
特别注意的是configure在SectionGroup的调用路径MySampleConfigurationSection myConfigSection = (MySampleConfigurationSection)ConfigurationManager.GetSection("mySampleConfigurationSectionGroup/mySampleConfigurationSection");
if (myConfigSection != null)
{
txtMyAttribute1.Text = myConfigSection.MyAttribute1.ToString();
txtMyChildAttribute1.Text = myConfigSection.MyAttributeElement.MyChildAttribute1.ToString();
txtMyChildAttribute2.Text = myConfigSection.MyAttributeElement.MyChildAttribute2.ToString();
}
else
{
MessageBox.Show("Cannot read the sample configuration value");
}
ConfigurationManager.GetSection("mySampleConfigurationSectionGroup/mySampleConfigurationSection")