我設計了一個自定義節處理程序之前,但我面臨的問題,我似乎無法想象。我有這樣的配置部分:自定義web.config節處理程序
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<configSections>
<section name="providers" requirePermission="false" type="MyProject.Configuration.ProvidersSection"/>
</configSections>
<providers>
<provider name="products">
<queries>
<add name="insert" value="InsertProduct" />
<add name="retrieve" value="GetProduct" />
<add name="collect" value="GetProducts" />
<add name="update" value="UpdateProduct" />
<add name="delete" value="DeleteProduct" />
<queries>
</provider>
<provider name="tasks">
<queries>
<add name="insert" value="InsertTask" />
<add name="retrieve" value="GetTaskById" />
<add name="collect" value="GetMultipleTasks" />
<add name="update" value="UpdateTask" />
<add name="delete" value="DeleteTask" />
<queries>
</provider>
<provider name="events">
<queries>
<add name="insert" value="AddEvent" />
<add name="retrieve" value="GetEvent" />
<add name="collect" value="GetEvents" />
<add name="update" value="UpdateEvent" />
<add name="delete" value="DeleteEvent" />
<queries>
</provider>
</providers>
</configuration>
我創建了以下處理類:
using System.Configuration;
namespace MyProject.Configuration
{
public class ProvidersSection : ConfigurationSection
{
public new Element this[string key]
{
get
{
}
}
}
[ConfigurationCollection(typeof(ProviderElement))]
public class ProvidersCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ProviderElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return element.ElementInformation.Properties["name"].Value;
}
public ProviderElement this[string key]
{
get
{
return (ProviderElement)base.BaseGet(key);
}
}
}
public class ProviderElement : ConfigurationElement
{
public string this[string name]
{
get
{
return string.Empty;
}
}
}
}
,我需要這些課程,以便成功地執行以下代碼的代碼是什麼?
string query = ProvidersSection["tasks"].Queries["Insert"];
謝謝你的及時迴應。我非常感謝你的有效方法。我設法弄清楚如何處理單個提供者中的集合。但是,如何使我的「ProvidersSection」類成爲SectionHandler以及ConfigurationElementCollection? – 2009-10-30 19:27:06
我想問一個問題的另一種方法是,「我如何創建一個ConfigurationElementCollection對象的集合映射到SectionHandler中的每個提供者? – 2009-10-30 19:42:52
用代碼編輯我的答案 – 2009-10-30 20:21:22