我为我的ConfigurationSection,ConfigurationElement(s)和ConfigurationElementCollection(s)创建了一个定制的app.config,它基于这份文件。
现在,我希望能够访问任何配置元素中的父元素。
例如,下面一行的内容:
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中分配该属性值。
发布于 2018-08-26 10:17:51
您没有遗漏ConfigurationElement中的任何内容,它无法提供任何层次结构或订单信息。您必须自己保存这些信息,例如,请参阅这个答案。
对于一般解决方案,您可能希望查看我的POC,以在app.config中定义父占位符。
顺便提一句,在以前的版本中,我使用了接口(您可以检查以前的提交)和当前版本中的扩展属性来完成这个任务。
另外,下面是一个精简的版本,它只完成了您的需求:
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;
}
}现在您可以在自定义部分中使用这些基本配置类,如下所示:
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 ...
}https://stackoverflow.com/questions/51878395
复制相似问题