Vous devez:
Définir my.configuration
comme groupe de sections et emailNotification
comme une section au sein du groupe. Ajouter suivante dans le fichier de configuration:
<configSections>
<sectionGroup name="my.configuration"
type="SectionGroupRetrieval.MyConfigurationGroup, SectionGroupRetrieval">
<section name="emailNotification"
type="SectionGroupRetrieval.EmailNotificationSection, SectionGroupRetrieval" />
</sectionGroup>
</configSections>
Mettre en oeuvre le groupe de section de configuration (my.configuration
).
public class MyConfigurationGroup : ConfigurationSectionGroup
{
[ConfigurationProperty("emailNotification")]
public EmailNotificationSection EmailNotification
{
get { return (EmailNotificationSection)base.Sections[ "emailNotification" ]; }
}
}
Implémentez la section de configuration (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" ]; }
}
}
Implémenter les éléments de configuration et la collection d'éléments de configuration nécessaires.
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;
}
}
Après la mise à jour du fichier de configuration et la mise en œuvre des bits de configuration nécessaires, vous pouvez vous accéder à la section comme suit:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
MyConfigurationGroup myConfiguration = (MyConfigurationGroup)config.GetSectionGroup("my.configuration");
EmailNotificationSection section = myConfiguration.EmailNotification;
Je suppose que le 'my.configuration' est un groupe et' emailNotification' est imbriquée section dont vous parlez. Correct? –
my.configuration est une section Et emailNotification du groupe, oui – chriszero
Je vais poster un exemple dans une minute. –