简体   繁体   English

在app.config中链接自定义配置部分

[英]Linking custom config sections in an app.config

My app.config has these custom sections: 我的app.config具有以下自定义部分:

<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 . 我有以下标准类: AnimalAnimalCollectionZooZooCollection

Zoo is exactly as expected, and looks like this: Zoo完全符合预期,看起来像这样:

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. 所以Zoo.AnimalsAnimal实例名称的CSV字符串。 But what I want is an IEnumerable<Animal> property. 但是我想要的是IEnumerable<Animal>属性。

How do I do that? 我怎么做?

Basically what you need is to provide a way to convert a comma-separated string value to IEnumerable. 基本上,您需要提供一种将逗号分隔的字符串值转换为IEnumerable的方法。 This can be achieved by providing a custom TypeConverter. 这可以通过提供自定义TypeConverter来实现。 So your Zoo class would look like this: 因此,您的Zoo类如下所示:

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: 而AnimalConverter是:

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. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM