我不确定您是否可以通过设计器生成的设置进行操作,但我不经常使用它们,因此我可能会出错。但是,还有另外一种方法可以做到这一点:创建自己的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>
有可能是在错别字,但总体思路是存在的。希望有所帮助。
为什么?它只需将接口添加到生成的设置类定义中。缺点是每次更改设置时都会覆盖它。无论如何,它仍然是完美的配色方案。 – Harry 2014-11-23 08:11:37