namespace provider.Provider
{
/// <summary>
/// 这个类可以看成是一个管理配置文件中节点的类,
/// 这个类中属性使用Attribute的方式声明值,具体里面的值可以通过反射得到
/// </summary>
public class ParentProviderConfigurationSection : ConfigurationSection
{
/// <summary>
/// 获取配置文件中配置节名称为Providers的信息
/// </summary>
[ConfigurationProperty("providers")]
public ProviderSettingsCollection Providers
{
get { return (ProviderSettingsCollection)base["providers"]; }
}
//[StringValidator(MinLength=1)]
[ConfigurationProperty("defaultProvider")]
public string DefaultProvider
{
get { return (string)base["defaultProvider"];}
set { base["defaultProvider"]=value;}
}
}
}
1.4创建ChineseProvider继承自ParentProvider
namespace provider.Provider
{
public class ChineseProvider:ParentProvider
{
public override string ShowData()
{
return "你好,欢迎来到中国";
}
}
}
1.5创建EnglishProvider继承自ParentProvider
namespace provider.Provider
{
public class EnglishProvider:ParentProvider
{
public override string ShowData()
{
return "hello ,welcome to china";
}
}
}
2.编译该类库项目生成程序集文件
3.新建c#窗体项目
3.1 添加providerDemo.dll的引用
3.2 添加引用System.Configuration
3.3 修改配置文件
<configuration>
<configSections>
<section name ="parentProvider" type ="provider.Provider.ParentProviderConfigurationSection,providerDemo"/>
</configSections>
<parentProvider defaultProvider="ChineseProvider">
<providers>
<add name="ChineseProvider" type="provider.Provider.ChineseProvider"/>
<add name="EnglishProvider" type="provider.Provider.EnglishProvider"/>
</providers>
</parentProvider>
</configuration>