您需要:
定義my.configuration
爲欄目組和emailNotification
爲組內的部分。添加下面的配置文件:
<configSections>
<sectionGroup name="my.configuration"
type="SectionGroupRetrieval.MyConfigurationGroup, SectionGroupRetrieval">
<section name="emailNotification"
type="SectionGroupRetrieval.EmailNotificationSection, SectionGroupRetrieval" />
</sectionGroup>
</configSections>
實現配置節組(my.configuration
)。
public class MyConfigurationGroup : ConfigurationSectionGroup
{
[ConfigurationProperty("emailNotification")]
public EmailNotificationSection EmailNotification
{
get { return (EmailNotificationSection)base.Sections[ "emailNotification" ]; }
}
}
執行配置部分(emailNotification
)。
public class EmailNotificationSection : ConfigurationSection
{
[ConfigurationProperty("to")]
public ValueElement To
{
get { return (ValueElement)base[ "to" ]; }
}
[ConfigurationProperty("from")]
public ValueElement From
{
get { return (ValueElement)base[ "from" ]; }
}
[ConfigurationProperty("subject")]
public ValueElement Subject
{
get { return (ValueElement)base[ "subject" ]; }
}
[ConfigurationProperty("smtpHost")]
public ValueElement SmtpHost
{
get { return (ValueElement)base[ "smtpHost" ]; }
}
[ConfigurationProperty("triggers")]
public TriggerElementCollection Triggers
{
get { return (TriggerElementCollection)base[ "triggers" ]; }
}
}
實現必要的配置元素和配置元素集合。
public class ValueElement : ConfigurationElement
{
[ConfigurationProperty("value")]
public string Value
{
get { return (string)base[ "value" ]; }
set { base[ "value" ] = value; }
}
}
public class TriggerElement : ConfigurationElement
{
[ConfigurationProperty("name")]
public string Name
{
get { return (string)base[ "name" ]; }
set { base[ "name" ] = value; }
}
[ConfigurationProperty("varAlias")]
public string VarAlias
{
get { return (string)base[ "varAlias" ]; }
set { base[ "varAlias" ] = value; }
}
[ConfigurationProperty("lower")]
public int Lower
{
get { return (int)base[ "lower" ]; }
set { base[ "lower" ] = value; }
}
[ConfigurationProperty("upper")]
public int Upper
{
get { return (int)base[ "upper" ]; }
set { base[ "upper" ] = value; }
}
}
[ConfigurationCollection(typeof(TriggerElement))]
public class TriggerElementCollection : ConfigurationElementCollection
{
public TriggerElement this[ string name ]
{
get { return (TriggerElement)base.BaseGet(name); }
}
public TriggerElement this[ int index ]
{
get { return (TriggerElement)base.BaseGet(index); }
}
protected override ConfigurationElement CreateNewElement()
{
return new TriggerElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((TriggerElement)element).Name;
}
}
更新配置文件和執行必要的配置位後,便可輸入部分,如下所示:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
MyConfigurationGroup myConfiguration = (MyConfigurationGroup)config.GetSectionGroup("my.configuration");
EmailNotificationSection section = myConfiguration.EmailNotification;
我假定`my.configuration`是一組和`emailNotification`是嵌套部分你指的是。正確? – 2011-02-17 10:34:41
my.configuration是一個節和emailNotification組,是 – chriszero 2011-02-17 11:22:08