App.configでそのようなネストされた構成セクションにアクセスする方法の例は見つかりません
<my.configuration>
<emailNotification>
<to value="[email protected]" />
<from value="[email protected]" />
<subject value="Subject" />
<smtpHost value="smtp.you.com" />
<triggers>
<add name="1" varAlias="Var1" lower="-200" upper="-150"/>
</triggers>
</emailNotification>
</my.configuration>
以前、ConfigurationElementCollectionとConfigurationElementを使用しました。しかし、私は上記を行う方法がわかりませんか?
必要がある:
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;