Sorry, I am new to config files and xml.
This examples is close to what I want: Correct implementation of a custom config section with nested collections? (state-county-example). Unfortunatelly code displayed there showes a lot of cs-files altogether in one big piece. Please don't blame me for that.
However, I did not find an example, using a config-file as below - where (in my case) element ExpectedColums can EITHER include colums containing an enumeration OR containing just any text with an optional prefix-text.
Example: Lets assume we got some kind of table and we want to configure a related parser.
<?xml version="1.0" encoding="utf-8" ?>
<TableParserConfig>
<ExpectedColumns>
<Column title="ID" type="string" isKeyColumn="Yes" isMandatory="Yes"/>
<Prefix text="any prefix">
</Column>
<Column title="She likes me" type="string" isKeyColumn="No" isMandatory="Yes"/>
<SingleState state="Undefined">
<SingleState state="No">
<SingleState state="Yes">
</Column>
</ExpectedColumns>
</TableParserConfig>
What I tried: Is close to example refered to by given link above. But I implemented an abstract base class and used their children to differ between Prefix elements and SingleState elements - both inheriting from class ConfigurationElement.
So classes of code below are analog class usaStateDefinitionConfigElement of code refered to by link above.
What I don't understand: I wonder how between both children can be differed by reflection, if both are obtaining the same text as string via 1th parameter of attribute?
My Questions:
- Is such an "OR-case" possible in an configuration-file?
- If "yes", how to implement related code behind?
- See "What I don't understand", above.
- If "no", how to achive an or-option, easy to understand (in the best case self-explaining) for a configurating programmer without big knowlege about code behind?
- If "yes", how to implement related code behind?
- Relating to the topic: Is there a general or significant difference between "possible layouts" in xml compared to config-files?
- If yes: Do I have to take a workaround by using xml-files? (using another namespace)
Part of my implementation:
using System.Configuration
namespace MyTableExample
{
public class SingleStateConfigElement : AbstractConfigElement
{
private const string _columnPropertyName = "ExpectedCollumns";
[ConfigurationProperty(_columnPropertyName, IsDefaultCollection = false)]
public override AbstractConfigElement ExpectedColumn
{
get { return (SingleStateConfigElement)base[_columnPropertyName]; }
set { this[_columnPropertyName] = value; }
}
}
public class CommonContentConfigElement : AbstractConfigElement
{
private const string _columnPropertyName = "ExpectedCollumns";
[ConfigurationProperty(_columnPropertyName, IsDefaultCollection = false)]
public override AbstractConfigElement ExpectedColumn
{
get { return (CommonContentConfigElement)base[_columnPropertyName]; }
set { this[_columnPropertyName] = value; }
}
}
public abstract class AbstractConfigElement : ConfigElement
{
...
private const string _isMandatory = "isMandatory";
...
[ConfigurationProperty(_isMandatory, IsRequired = false)]
public bool IsMandatory
{
get { return (bool)base[_isMandatory]; }
set { this[_isMandatory] = value; }
}
public abstracte AbstractConfigElement ExpectedColumn { get; set;}
}
}