I have created a custom ConfigurationSection
, ConfigurationElement(s)
and ConfigurationElementCollection(s)
for my app.config, it's based on this documentation.
Now I would like to be able to access a parent element in any configuration element.
For example something in the lines of the following:
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?
}
}
Is there something in the base ConfigurationElement
class that I'm missing and that would enable me to do this?
I'm hoping if it's possible to achieve this with some kind of generic solution;
One that would not require to introduce something like a Parent
property on each element and then need to assign that property value in each ConfigurationProperty
getter.