自定义配置设置问题

时间:2010-11-25 22:25:23

标签: winforms configuration app-config custom-configuration

我在我的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;
            }
        }
}
}

哪个不行。显然它不理解'属性'语法。

任何想法我做错了什么?感谢。

1 个答案:

答案 0 :(得分:1)

如果要以这种方式定义您需要的属性:

<CustomFields>
    <property name="setting1">hello</property>
    <property name="setting2">world</property>
    ...
</CustomFields>

比每个“property”成为CustomFields的子节点,您的属性现在是这些子节点的属性/值,而不是第一个示例中的CustomFields节点的属性。

如果您有很多属性,并且想要更优雅地设置它们,可以考虑两个选项:

1)对自定义部分使用以下结构(略有更改):

<CustomFields>
    <setting1 value="hello"/>
    <setting2 value="world"/>
    ...   
</CustomFields>

和以下代码定义用于检索值的属性:

public class CustomFields: ConfigurationSection
{            
    [ConfigurationProperty("setting1")]
    public PropertyElement Setting1
    {
        get
        {
            return (PropertyElement)this["setting1"];
        }
        set
            { this["setting1"] = value; }
    }

    [ConfigurationProperty("setting2")]
    public PropertyElement Setting2
    {
        get
        {
            return (PropertyElement)this["setting2"];
        }
        set
            { this["setting2"] = value; }
    }
}  
public class PropertyElement : ConfigurationElement
{
    [ConfigurationProperty("value", IsRequired = false)]
    public String Value
    {
        get
        {
            return (String)this["value"];
        }
        set
        {
            this["value"] = value;
        }
    }
}

然后,检索值:

string setting1value = myCustomFields.Setting1.Value;
string setting2value = myCustomFields.Setting2.Value;

有关详细信息,请参阅MSDN上的How to: Create Custom Configuration Sections Using ConfigurationSection

2)采用程序化方法,而不是依赖属性和反射。在这种情况下,可以使用ConfigurationSection classIConfigurationSectionHandler。因此,您将可以从代码访问包含自定义部分数据的xml节点,并且可以加载任何类型的XML结构。

相关问题