gpt4 book ai didi

c# - 嵌套配置部分 app.config

转载 作者:IT王子 更新时间:2023-10-29 04:42:15 24 4
gpt4 key购买 nike

我没有找到任何有关如何在 app.config 中访问此类嵌套配置部分的示例

  <my.configuration>
<emailNotification>
<to value="me@you.com" />
<from value="he@you.com" />
<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;

关于c# - 嵌套配置部分 app.config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5027284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com