name為自定義節點的名稱,type為自定義節點決議檔案的命名空間和自定義節處理程式的類名
1. 在<configSections>節點中注冊你所要定義的節點名稱及用于處理該節點的配置節處理程式,代碼如下:
<configSections> <section name="dbFactory" type="DbFactory.Configuration.DbFactorySection,DbFactory.Configuration"/> </configSections>
2. 在適當的位置添加自定義的節點,代碼如下:
<configSections>
<section name="dbFactory" type="DbFactory.Configuration.DbFactorySection,DbFactory.Configuration"</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>
public class DefaultElement : ConfigurationElement
{
[ConfigurationProperty("factory")]
public string Factory
{
get
{
return this["factory"] as string;
}
set
{
this["factory"] = value;
}
}
}
public class FactoryElement : ConfigurationElement
{
[ConfigurationProperty("name")]
public string Name
{
get
{
return this["name"] as string;
}
set
{
this["name"] = value;
}
}
[ConfigurationProperty("assembly")]
public string Assembly
{
get
{
return this["assembly"] as string;
}
set
{
this["assembly"] = value;
}
}
[ConfigurationProperty("class")]
public string Class
{
get
{
return this["class"] as string;
}
set
{
this["class"] = value;
}
}
}
public class FactoryElements : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new FactoryElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((FactoryElement)element).Name;
}
public FactoryElement this[string name]
{
get
{
return BaseGet(name) as FactoryElement;
}
}
}
public class DbFactorySection : ConfigurationSection
{
[ConfigurationProperty("default")]
public DefaultElement DefaultFactory
{
get
{
return this["default"] as DefaultElement;
}
set
{
this["default"] = value;
}
}
[ConfigurationProperty("factorys")]
public FactoryElements Factorys
{
get
{
return this["factorys"] as FactoryElements;
}
set
{
this["factorys"] = value;
}
}
}
原文鏈接:
http://www.cnblogs.com/amylis_chen/p/5985054.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/80513.html
標籤:C#
上一篇:Oracle.DataAccess.Client.OracleConnection”的型別初始值設定項引發例外
下一篇:MD5加密
