如何在后代元素上使用 .NET 自定义 ConfigurationElement 属性? [英] How do I use .NET custom ConfigurationElement properties on descendent elements?

查看:22
本文介绍了如何在后代元素上使用 .NET 自定义 ConfigurationElement 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取和使用在后代 CustomSetting 元素的父 ConfigurationSection 中设置的属性?当 CustomSetting 元素返回 Value 属性时,我需要这个属性.

How can I get and use an attribute set in the parent ConfigurationSection in the descendent CustomSetting element? I need this attribute when the CustomSetting element is returning the Value property.

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

I want to format the App.config like this:

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

我的代码可以工作,只是我找不到从 CustomSetting 类访问 someProperty 属性的方法.到目前为止,我发现的唯一方法是像这样格式化配置,这很混乱:

I have the code working, except that I cannot find a way to access the someProperty attribute from the CustomSetting class. The only way that I've found, so far, is to format the configuration like this, which is messy:

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

推荐答案

实现这一点比应有的要困难得多,因为 System.Configuration API 不允许您从 ConfigurationElement 导航到它的父级.因此,如果您想访问父元素上的某些信息,您需要手动创建该关系.我已经为您问题中的配置片段汇总了一个示例实现:

Achieving this is more difficult than it should be since the System.Configuration API doesn't allow you to navigate from a ConfigurationElement to its parent. Hence, if you want to access some information that on a parent element you need to create that relationship manually. I've put together a sample implementation that does that for the config snippet in your question:

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 获取器中设置.CustomSettingElement 又具有 Parent 属性,该属性在集合的 CreateNewElement() 方法中设置.

You can see that the CustomSettingElementCollection has a Section property which gets set in the section's Elements getter. The CustomSettingElement, in turn, has a Parent property which gets set in the collection's CreateNewElement() method.

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

That then makes it possible to walk up the relationship tree and to add a SomeProperty property to the element even though this one doesn't correspond to an actual ConfigurationProperty on that element.

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

Hope that gives you an idea how to solve your problem!

这篇关于如何在后代元素上使用 .NET 自定义 ConfigurationElement 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆