我创建了一个自定义ConfigurationSection
,对于我的app.configConfigurationElement(s)
,它基于此文档。ConfigurationElementCollection(s)
现在我希望能够访问任何配置元素中的父元素。
例如以下行中的内容:
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 中分配该属性值。