自定义配置设置问题
我向 Windows 窗体应用程序的 app.config 文件添加了一个自定义部分。我创建了类来扩展配置文件:
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;
}
}
}
}
这不起作用。显然它不理解“属性”语法。
有什么想法我做错了吗?谢谢。
I added a custom section to my app.config file for a Windows Forms Application. I created the class to extend the configuration file:
CustomFields myCustomFields = (CustomFields)System.Configuration.ConfigurationManager.GetSection("CustomFields");
I specify the section name:
<section name="CustomFields" type="Application.Core.CustomFields, ATMCardRequest.Core" allowLocation="true" allowDefinition="Everywhere" />
Now here is where I think the issue is. The above has worked fine before but I need a lot of properties for this section and instead of doing this:
<CustomFields setting1='hello' setting2='world'/>
I am doing this:
<CustomFields>
<property name="setting1">hello</property>
<property name="setting2">world</property>
...
</CustomFields>
Code:
/// <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;
}
}
}
}
Which isn't working. Apparently it doesn't understand the 'property' syntax.
Any ideas what I am doing wrong? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果以这种方式定义您需要的属性:
那么每个“属性”都会成为 CustomFields 的子节点,并且您的属性现在是这些子节点的属性/值,而不是第一个示例中的 CustomFields 节点的属性。
如果您有很多属性并且想要更优雅地设置它们,可以考虑以下两个选项:
1) 对自定义部分使用以下结构(略有更改):
并使用以下代码来定义用于检索值的属性:
然后,检索值:
有关详细信息,请参阅 如何:使用 MSDN 上的 ConfigurationSection 创建自定义配置节。
2) 采用编程方法,而不是依赖属性和反射。 ConfigurationSection 类 或 IConfigurationSectionHandler。因此,您将可以从代码访问包含自定义部分数据的 xml 节点,并且能够加载任何类型的 XML 结构。
If would define the properties that you need this way:
than each "property" becomes child node for CustomFields and your properties are now attributes/values for those child nodes, not attributes of the CustomFields node as in the first example.
If you have lot of properties and you want to set them more elegantly here are two options that might consider:
1) Use the following structure for the custom section (slightly changed):
and the following code to define the properties used to retrieve the values:
Then, to retrieve the values:
For details please see How to: Create Custom Configuration Sections Using ConfigurationSection on MSDN.
2) Take a programmatic approach instead of relying on attributes and reflection. ConfigurationSection class or IConfigurationSectionHandler could be used in this case. As a result you will have access from code to the xml node containing the custom section data and will be able to load any kind of XML structure.