0

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?
  • 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;}

   }
}
B. Ernst
  • 25
  • 4
  • Do you know of any configuration in the framework that behaves like what you're trying to achieve? – Paulo Morgado Mar 26 '20 at 10:39
  • Thanks, Paolo Morgado, for your hint to come forwart - as we all sometimes don't see the forrest because of a lot of trees. Due to missing replies I think the answer to my question is "no". My 'workaround': A further enum, similar granadaCoder already implemented one in his example (of link above). In my case this further enum differs between SingleState and CommonContent. – B. Ernst Mar 27 '20 at 10:33
  • I haven't done any configuration code in years, but you can find implementations on https://referencesource.microsoft.com/ – Paulo Morgado Mar 27 '20 at 10:43
  • After looking through several config-files and xml-files: Nope. I stick to my questions above. There must be a way to put different elements into collections of any kind ofelement. Or at least config-files can look like this. – B. Ernst Mar 27 '20 at 14:58

0 Answers0