代码之家  ›  专栏  ›  技术社区  ›  Damien

自定义配置设置有问题

  •  0
  • Damien  · 技术社区  · 15 年前

    我为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;
                }
            }
    }
    }
    

    这不管用。显然它不理解“property”语法。

    你知道我做错了什么吗?谢谢。

    1 回复  |  直到 15 年前
        1
  •  1
  •   andrei m    15 年前

    If将以这种方式定义所需的属性:

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

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

    如果您有很多属性,并且希望对它们进行更优雅的设置,可以考虑以下两个选项:

    (一) 对自定义节使用以下结构(略有更改):

    <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;
    

    详情请参阅 How to: Create Custom Configuration Sections Using ConfigurationSection 在MSDN上。

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