首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ConfigurationElement中获取app.config父级

在ConfigurationElement中获取app.config父级
EN

Stack Overflow用户
提问于 2018-08-16 13:35:32
回答 1查看 912关注 0票数 1

我为我的ConfigurationSectionConfigurationElement(s)ConfigurationElementCollection(s)创建了一个定制的app.config,它基于这份文件

现在,我希望能够访问任何配置元素中的父元素。

例如,下面一行的内容:

代码语言:javascript
复制
public class CustomSection : ConfigurationSection
{
    [ConfigurationProperty("child")]
    public ChildElement Child
    {
        get { return (ChildElement)this["child"]; }
        set { this["child"] = value; }
    }
}

public class ChildElement : ConfigurationElement
{
    [ConfigurationProperty("name")]
    public string Name
    {
        get { return (string)this["name"]; }
        set { this["name"] = value; }
    }

    [ConfigurationProperty("nestedchild")]
    public NestedChildElement NestedChild
    {
        get { return (NestedChildElement)this["nestedchild"]; }
        set { this["nestedchild"] = value; }
    }
}

public class NestedChildElement : ConfigurationElement
{
    [ConfigurationProperty("name")]
    public string Name
    {
        get { return (string)this["name"]; }
        set { this["name"] = value; }
    }

    public void Sample()
    {
        // How can I access parent ChildElement object
        // and also its parent CustomSection object from here?
    }
}

基类ConfigurationElement中有什么东西是我遗漏的,可以让我这样做吗?

我希望如果有可能通过某种通用的解决方案来实现这一点;

它不需要在每个元素上引入类似Parent属性的内容,然后需要在每个ConfigurationProperty getter中分配该属性值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-26 10:17:51

您没有遗漏ConfigurationElement中的任何内容,它无法提供任何层次结构或订单信息。您必须自己保存这些信息,例如,请参阅这个答案

对于一般解决方案,您可能希望查看我的POC,以在app.config中定义父占位符。

顺便提一句,在以前的版本中,我使用了接口(您可以检查以前的提交)和当前版本中的扩展属性来完成这个任务。

另外,下面是一个精简的版本,它只完成了您的需求:

代码语言:javascript
复制
public abstract class ConfigurationElementBase : ConfigurationElement
{
    protected T GetElement<T>(string name) where T : ConfigurationElement
        => this.GetChild<T>(name);
}

public abstract class ConfigurationSectionBase : ConfigurationSection
{
    protected T GetElement<T>(string name) where T : ConfigurationElement
        => this.GetChild<T>(name);
}

public static class ConfigurationExtensions
{
    private static readonly Dictionary<ConfigurationElement, ConfigurationElement> Parents =
        new Dictionary<ConfigurationElement, ConfigurationElement>();

    public static T GetParent<T>(this ConfigurationElement element) where T : ConfigurationElement
        => (T)Parents[element];

    private static void SetParent(this ConfigurationElement element, ConfigurationElement parent)
        => Parents.Add(element, parent);

    private static object GetValue(this ConfigurationElement element, string name)
        => element.ElementInformation.Properties.Cast<PropertyInformation>().First(p => p.Name == name).Value;

    internal static T GetChild<T>(this ConfigurationElement element, string name) where T : ConfigurationElement
    {
        T childElement = (T)element.GetValue(name);
        if (!Parents.ContainsKey(childElement))
            childElement.SetParent(element);
        return childElement;
    }
}

现在您可以在自定义部分中使用这些基本配置类,如下所示:

代码语言:javascript
复制
public class CustomSection : ConfigurationSectionBase
{
    [ConfigurationProperty("name")]
    public string Name
    {
        get { return (string)this["name"]; }
        set { this["name"] = value; }
    }

    [ConfigurationProperty("child")]
    public ChildElement Child => base.GetElement<ChildElement>("child");
}

public class ChildElement : ConfigurationElementBase
{
    [ConfigurationProperty("name")]
    public string Name
    {
        get { return (string)this["name"]; }
        set { this["name"] = value; }
    }

    [ConfigurationProperty("nestedchild")]
    public NestedChildElement NestedChild => base.GetElement<NestedChildElement>("nestedchild");
}

public class NestedChildElement : ConfigurationElement
{
    [ConfigurationProperty("name")]
    public string Name
    {
        get { return (string)this["name"]; }
        set { this["name"] = value; }
    }

    public void Sample()
    {
        ChildElement parentChildElement = this.GetParent<ChildElement>();
        CustomSection parentCustomSection = parentChildElement.GetParent<CustomSection>();
        // TODO Use the parents ...
    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51878395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档