我的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>
我有以下标准类:Animal
,AnimalCollection
,Zoo
,ZooCollection
。
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 ...?
*/
}
因此Zoo.Animals
是Animal
个实例名称的CSV字符串。但我想要的是一个IEnumerable<Animal>
属性。
我该怎么做?
答案 0 :(得分:1)
基本上你需要的是提供一种将逗号分隔的字符串值转换为IEnumerable的方法。这可以通过提供自定义TypeConverter来实现。所以你的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"]; }
}
}
并且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;
}
}
当我使用控制台应用程序测试时:
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();
}
}
}
我得到了这些结果:
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
我希望这会有所帮助。