我不确定你是否可以通过设计器生成的设置来做你想做的事,但我不经常使用它们,所以我可能是错的。但是,还有另一种方法可以做到这一点:创建自己的ConfigurationSection。
这是一个例子:
public class MyProperties : ConfigurationSection {
[ConfigurationProperty("A")]
public MySettings A
{
get { return (MySettings )this["A"]; }
set { this["A"] = value; }
}
[ConfigurationProperty("B")]
public MySettings B
{
get { return (MySettings )this["B"]; }
set { this["B"] = value; }
}
}
public class MySettings : ConfigurationElement {
[ConfigurationProperty("greeting")]
public string Greeting
{
get { return (string )this["greeting"]; }
set { this["greeting"] = value; }
}
}
然后您的 app.config/web.config 需要以下内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="mySettings" type="Namespace.MyProperties, Assembly"/>
</configSections>
<mySettings>
<A greeting="Hello from A!" />
<B greeting="Hello from B" />
</mySettings>
</configuration>
可能有错别字,但总体思路就在那里。希望有帮助。