我在我的 app.config 文件中为 Windows 窗体应用程序添加了一个自定义部分。我创建了类来扩展配置文件:
CustomFields myCustomFields = (CustomFields)System.Configuration.ConfigurationManager.GetSection("CustomFields");
我指定部分名称:
<section name="CustomFields" type="Application.Core.CustomFields, ATMCardRequest.Core" allowLocation="true" allowDefinition="Everywhere" />
现在这就是我认为问题所在。上面的内容之前运行良好,但我需要为这一部分设置很多属性,而不是这样做:
<CustomFields setting1='hello' setting2='world'/>
我正在这样做:
<CustomFields>
<property name="setting1">hello</property>
<property name="setting2">world</property>
...
</CustomFields>
代码:
/// <summary>
/// Settings file which holds the name of the XML Fields
/// </summary>
public class setting1: ConfigurationSection
{
/// <summary>
/// Name of the setting1 Field
/// </summary>
[ConfigurationProperty("setting1", IsRequired = true)]
public String setting1
{
get
{
return (String)this["setting1"];
}
set
{
this["setting1"] = value;
}
}
/// <summary>
/// Name of the setting2 Field
/// </summary>
[ConfigurationProperty("setting2",IsRequired = true)]
public String setting2
{
get
{
return (String)this["setting2"];
}
set
{
this["setting2"] = value;
}
}
}
}
这是行不通的。显然它不理解“属性”语法。
任何想法我做错了什么?谢谢。