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.
You haven't missed anything in
ConfigurationElement
, it's unable to give you any hierarchy or order information. You'll have to keep this information yourself, for example see this answer.For a generic solution you may want to check out my POC for defining parent placeholders in app.config.
As a side note, in previous version I've done this with interface (you could check previous commits) and in the current version with extension property.
Also, the following is a trimmed down version that accomplishes just your requirement:
Now you can use these base configuration classes in your custom section as following: