h bob
h bob

Reputation: 3780

Linking custom config sections in an app.config

My app.config has these custom sections:

<Animals>
  <add Type="Elephant" Name="Dumbo" Age="22" />
  <add Type="Giraffe" Name="Shorty" Age="5" />
  <add Type="Giraffe" Name="Mike" Age="7" />
  <add Type="Lion" Name="Shaggy" Age="2" />
</Animals>


<Zoos>
  <add Name="New York Zoo" Animals="Dumbo,Shorty" />
  <add Name="Paris Zoo" Animals="Mike,Shaggy" />
</Zoos>

I have these standard classes: Animal, AnimalCollection, Zoo, ZooCollection.

Zoo is exactly as expected, and looks like this:

public class Zoo : ConfigurationElement {

  [ConfigurationProperty("Name", IsRequired=true)]
  public string Name { get { return base["Name"] as string; } }

  // I don't want this:
  [ConfigurationProperty("Animals", IsRequired=true)]
  public string Animals { get { return base["Animals"] as string; } }

  // I want something like this:
  /*
  [ConfigurationProperty("Animals", IsRequired=true)]
  public IEnumerable<Animal> Animals { get { return ...?
  */

}

So Zoo.Animals is a CSV string of names of Animal instances. But what I want is an IEnumerable<Animal> property.

How do I do that?

Upvotes: 0

Views: 80

Answers (1)

Volkan Paksoy
Volkan Paksoy

Reputation: 6957

Basically what you need is to provide a way to convert a comma-separated string value to IEnumerable. This can be achieved by providing a custom TypeConverter. So your Zoo class would look like this:

public class Zoo : ConfigurationElement
{
    [ConfigurationProperty("Name")]
    public string Name
    {
        get { return (string)base["Name"]; }
    }

    [TypeConverter(typeof(AnimalConverter))]
    [ConfigurationProperty("Animals")]
    public IEnumerable<Animal> Animals
    {
        get { return (IEnumerable<Animal>)base["Animals"]; }
    }
}

and the AnimalConverter is:

public class AnimalConverter : TypeConverter
{
    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        AnimalCollection animals = AnimalSettings.Settings.AnimalList;
        List<Animal> result = new List<Animal>();

        foreach (string animalName in value.ToString().Split(','))
        {
            foreach (Animal animal in animals)
            {
                if (animalName == animal.Name)
                {
                    result.Add(animal);
                }
            }
        }

        return result;
    }
}

When I tested it with a console app:

class Program
{
    static void Main(string[] args)
    {
        AnimalCollection animals = AnimalSettings.Settings.AnimalList;
        foreach (Animal animal in animals)
        {
            Console.WriteLine($"{animal.Name}\t\t{animal.Age}\t\t{animal.Type}");
        }
        Console.WriteLine();

        ZooCollection zoos = ZooSettings.Settings.ZooList;
        foreach (Zoo zoo in zoos)
        {
            Console.WriteLine(zoo.Name);
            foreach (Animal animal in zoo.Animals)
            {
                Console.WriteLine($"{animal.Name}\t\t{animal.Age}\t\t{animal.Type}" );
            }
            Console.WriteLine();
        }
    }
}

I got these results:

Dumbo           22              Elephant
Shorty          5               Giraffe
Mike            7               Giraffe
Shaggy          2               Lion

New York Zoo
Dumbo           22              Elephant
Shorty          5               Giraffe

Paris Zoo
Mike            7               Giraffe
Shaggy          2               Lion

I hope this helps.

Upvotes: 1

Related Questions