如何在后代元素上使用.NET自定义ConfigurationElement属性?

时间:2011-08-12 18:45:31

标签: .net configuration app-config configurationsection configurationelement

如何在后代CustomSetting元素中获取和使用父ConfigurationSection中的属性集?当CustomSetting元素返回Value属性时,我需要此属性。

我想像这样格式化App.config:

<CustomSettings someProperty="foo">
    <CustomSetting key="bar" value="fermeneba" />
    <CustomSetting key="laa" value="jubaduba" />
</CustomSettings>

我有代码工作,除了我找不到从CustomSetting类访问someProperty属性的方法。到目前为止,我发现的唯一方法是格式化这样的配置,这很麻烦:

<CustomSettings>
    <CustomSetting someProperty="foo" key="bar" value="fermeneba" />
    <CustomSetting someProperty="foo" key="laa" value="jubaduba" />
</CustomSettings>

1 个答案:

答案 0 :(得分:12)

由于System.Configuration API不允许您从ConfigurationElement导航到其父级,因此实现此操作比应该更加困难。因此,如果要访问父元素上的某些信息,则需要手动创建该关系。我已经为您的问题中的配置代码段整理了一个示例实现:

public class CustomSettingsSection : ConfigurationSection
{
    [ConfigurationProperty("someProperty", DefaultValue="")]
    public string SomeProperty
    {
        get { return (string)base["someProperty"]; }
        set { base["someProperty"] = value; }
    }

    [ConfigurationProperty("", IsDefaultCollection = true)]
    public CustomSettingElementCollection Elements
    {
        get 
        {
            var elements = base[""] as CustomSettingElementCollection;
            if (elements != null && elements.Section == null)
                elements.Section = this;
            return elements;
        }
    }
}

public class CustomSettingElementCollection : ConfigurationElementCollection
{

    internal CustomSettingsSection Section { get; set; }

    public override ConfigurationElementCollectionType CollectionType
    {
        get { return ConfigurationElementCollectionType.BasicMap; }
    }

    public CustomSettingElement this[string key]
    {
        get { return BaseGet(key) as CustomSettingElement; }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new CustomSettingElement { Parent = this };
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return (element as CustomSettingElement).Key;
    }

    protected override string ElementName
    {
        get { return "customSetting"; }
    }
}

public class CustomSettingElement : ConfigurationElement
{

    internal CustomSettingElementCollection Parent { get; set; }

    public string SomeProperty
    {
        get
        {
            if (Parent != null && Parent.Section != null)
                return Parent.Section.SomeProperty;
            return default(string);
        }
    }




    [ConfigurationProperty("key", IsKey = true, IsRequired = true)]
    public string Key
    {
        get { return (string)base["key"]; }
        set { base["key"] = value; }
    }

    [ConfigurationProperty("value", DefaultValue = "")]
    public string Value
    {
        get { return (string)base["value"]; }
        set { base["value"] = value; }
    }

}

您可以看到CustomSettingElementCollection具有Section属性,该属性在部分的Elements getter中设置。反过来,CustomSettingElement具有Parent属性,该属性在集合的CreateNewElement()方法中设置。

这样就可以走向关系树并向元素添加SomeProperty属性,即使这个属性与该元素上的实际ConfigurationProperty不对应。

希望能让您了解如何解决问题!