先要写配置类,然后封装成DLL


namespace
CustomConfig
{
public class DbFactorySection:System.Configuration.ConfigurationSection
{
[System.Configuration.ConfigurationProperty( " default " )]
public DefaultElement DefaultFactory
{
get
{
return this [ " default " ] as DefaultElement;
}
set
{
this [ " default " ] = value;
}
}
[System.Configuration.ConfigurationProperty( " factorys " )]
public FactoryElements Factorys
{
get
{
return this [ " factorys " ] as FactoryElements;
}
set
{
this [ " factorys " ] = value;
}
}
}
}
{
public class DbFactorySection:System.Configuration.ConfigurationSection
{
[System.Configuration.ConfigurationProperty( " default " )]
public DefaultElement DefaultFactory
{
get
{
return this [ " default " ] as DefaultElement;
}
set
{
this [ " default " ] = value;
}
}
[System.Configuration.ConfigurationProperty( " factorys " )]
public FactoryElements Factorys
{
get
{
return this [ " factorys " ] as FactoryElements;
}
set
{
this [ " factorys " ] = value;
}
}
}
}


namespace
CustomConfig
{
public class DefaultElement: System.Configuration.ConfigurationElement
{
[System.Configuration.ConfigurationProperty( " factory " )]
public string Factory
{
get
{
return this [ " factory " ] as string ;
}
set
{
this [ " factory " ] = value;
}
}
}
}
{
public class DefaultElement: System.Configuration.ConfigurationElement
{
[System.Configuration.ConfigurationProperty( " factory " )]
public string Factory
{
get
{
return this [ " factory " ] as string ;
}
set
{
this [ " factory " ] = value;
}
}
}
}


namespace
CustomConfig
{
public class FactoryElement:System.Configuration.ConfigurationElement
{
[System.Configuration.ConfigurationProperty( " name " )]
public string Name
{
get
{
return this [ " name " ] as string ;
}
set
{
this [ " name " ] = value;
}
}
[System.Configuration.ConfigurationProperty( " assembly " )]
public string Assembly
{
get
{
return this [ " assembly " ] as string ;
}
set
{
this [ " assembly " ] = value;
}
}
[System.Configuration.ConfigurationProperty( " class " )]
public string Class
{
get
{
return this [ " class " ] as string ;
}
set
{
this [ " class " ] = value;
}
}
}
}
{
public class FactoryElement:System.Configuration.ConfigurationElement
{
[System.Configuration.ConfigurationProperty( " name " )]
public string Name
{
get
{
return this [ " name " ] as string ;
}
set
{
this [ " name " ] = value;
}
}
[System.Configuration.ConfigurationProperty( " assembly " )]
public string Assembly
{
get
{
return this [ " assembly " ] as string ;
}
set
{
this [ " assembly " ] = value;
}
}
[System.Configuration.ConfigurationProperty( " class " )]
public string Class
{
get
{
return this [ " class " ] as string ;
}
set
{
this [ " class " ] = value;
}
}
}
}


namespace
CustomConfig
{
public class FactoryElements: System.Configuration.ConfigurationElementCollection
{
protected override System.Configuration.ConfigurationElement CreateNewElement()
{
return new FactoryElement();
}
protected override object GetElementKey(System.Configuration.ConfigurationElement element)
{
return ((FactoryElement)element).Name;
}
/// <summary>
/// 用于在元素集合中指定名字
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public FactoryElement this [ string name]
{
get
{
return BaseGet(name) as FactoryElement;
}
}
}
}
{
public class FactoryElements: System.Configuration.ConfigurationElementCollection
{
protected override System.Configuration.ConfigurationElement CreateNewElement()
{
return new FactoryElement();
}
protected override object GetElementKey(System.Configuration.ConfigurationElement element)
{
return ((FactoryElement)element).Name;
}
/// <summary>
/// 用于在元素集合中指定名字
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public FactoryElement this [ string name]
{
get
{
return BaseGet(name) as FactoryElement;
}
}
}
}
在app.config中加入以下节点:


<
configuration
>
< configSections >
< section name = " dbFactory " type = " CustomConfig.DbFactorySection,CustomConfig " />
</ configSections >
< dbFactory >
< default factory = " sql " >
</ default >
< factorys >
< add name = " sql " assembly = " Hello.Data " class = " SqlDbFactory " />
< add name = " oracle " assembly = " Hello.Data " class = " OracleDbFactory " />
</ factorys >
</ dbFactory >
< configSections >
< section name = " dbFactory " type = " CustomConfig.DbFactorySection,CustomConfig " />
</ configSections >
< dbFactory >
< default factory = " sql " >
</ default >
< factorys >
< add name = " sql " assembly = " Hello.Data " class = " SqlDbFactory " />
< add name = " oracle " assembly = " Hello.Data " class = " OracleDbFactory " />
</ factorys >
</ dbFactory >
后台中调用:
DbFactorySection conSection = System.Configuration.ConfigurationManager.GetSection("dbFactory") as CustomConfig.DbFactorySection;
string strName = conSection.Factorys["sql"].Name.ToString();
可以得到配置节点的信息.
转自:http://www.cnblogs.com/zhaoguihua/archive/2010/01/21/1652847.html