Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在app.config中获取ConfigurationElement父级_C#_App Config_Configurationsection_Configurationelement - Fatal编程技术网

C# 在app.config中获取ConfigurationElement父级

C# 在app.config中获取ConfigurationElement父级,c#,app-config,configurationsection,configurationelement,C#,App Config,Configurationsection,Configurationelement,我已经为我的app.config创建了一个自定义的ConfigurationSection,ConfigurationElement和ConfigurationElementCollection,它基于 现在,我希望能够访问任何配置元素中的父元素。 例如,以下几行中的某些内容: public class CustomSection : ConfigurationSection { [ConfigurationProperty("child")] public ChildEleme

我已经为我的app.config创建了一个自定义的
ConfigurationSection
ConfigurationElement
ConfigurationElementCollection
,它基于

现在,我希望能够访问任何配置元素中的父元素。
例如,以下几行中的某些内容:

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?
    }
}
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 ...
    }
在base
ConfigurationElement
类中是否有我缺少的东西可以让我这样做

我希望通过某种通用解决方案能够实现这一点

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

您在
ConfigurationElement
中没有遗漏任何内容,它无法为您提供任何层次结构或顺序信息。例如,您必须自己保存这些信息,请参见

对于通用解决方案,您可能希望在app.config中查看定义父占位符的信息。
作为补充说明,在以前的版本中,我已经使用接口(您可以检查以前的提交)和当前版本中的extension属性完成了这项工作

此外,以下是一个精简版,可满足您的要求:

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;
    }
}
公共抽象类ConfigurationElementBase:ConfigurationElement
{
受保护的T GetElement(字符串名称),其中T:ConfigurationElement
=>this.GetChild(名称);
}
公共抽象类ConfigurationSectionBase:ConfigurationSection
{
受保护的T GetElement(字符串名称),其中T:ConfigurationElement
=>this.GetChild(名称);
}
公共静态类配置扩展
{
私有静态只读字典父级=
新字典();
公共静态T GetParent(此ConfigurationElement元素),其中T:ConfigurationElement
=>(T)父元素[元素];
私有静态void SetParent(此ConfigurationElement元素,ConfigurationElement父元素)
=>父项。添加(元素,父项);
私有静态对象GetValue(此ConfigurationElement元素,字符串名称)
=>element.ElementInformation.Properties.Cast().First(p=>p.Name==Name).Value;
内部静态T GetChild(此ConfigurationElement元素,字符串名称),其中T:ConfigurationElement
{
T childElement=(T)element.GetValue(name);
如果(!Parents.ContainsKey(childElement))
SetParent(元素);
返回子元素;
}
}
现在,您可以在自定义部分中使用这些基本配置类,如下所示:

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?
    }
}
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 ...
    }
公共类CustomSection:ConfigurationSectionBase
{
[配置属性(“名称”)]
公共字符串名
{
获取{return(string)this[“name”];}
设置{this[“name”]=value;}
}
[配置属性(“子项”)]
public ChildElement Child=>base.GetElement(“Child”);
}
公共类ChildElement:ConfigurationElementBase
{
[配置属性(“名称”)]
公共字符串名
{
获取{return(string)this[“name”];}
设置{this[“name”]=value;}
}
[配置属性(“嵌套子项”)]
public NestedChildElement NestedChild=>base.GetElement(“NestedChild”);
}
公共类NestedChildElement:ConfigurationElement
{
[配置属性(“名称”)]
公共字符串名
{
获取{return(string)this[“name”];}
设置{this[“name”]=value;}
}
公开作废样本()
{
ChildElement parentChildElement=this.GetParent();
CustomSection parentCustomSection=parentChildElement.GetParent();
//要使用家长。。。
}
“ChildElement”类不是“NestedChildElement”类的父类。请注意,“ChildElement”包含一个“ConfigurationProperty”或“NestedChildElement”。在“app.config”中,这是一个“nestedchild”XML元素,它是“child”XML元素的父元素。我希望这能解释问题。