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

Jer*_*her 7 .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>
Run Code Online (Sandbox Code Playgroud)

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

<CustomSettings>
    <CustomSetting someProperty="foo" key="bar" value="fermeneba" />
    <CustomSetting someProperty="foo" key="laa" value="jubaduba" />
</CustomSettings>
Run Code Online (Sandbox Code Playgroud)

mth*_*rba 13

由于System.Configuration API不允许您从a导航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; }
    }

}
Run Code Online (Sandbox Code Playgroud)

你可以看到它CustomSettingElementCollection有一个Section在section的Elementsgetter中设置的属性.的CustomSettingElement,反过来,拥有Parent这被在收集的设置属性CreateNewElement()的方法.

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

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