我有一个包含对象列表成员的类。假设我有一个 Animal 基类和 Cat 和 Dog 的子类型。该列表将始终只包含狗或猫,但我不知道。
class MyClass {
public readonly List<Animal> Data = new();
public void MethodForDogs()
{
Data.Add(new Dog());
}
public void MethodForCats()
{
Data.Add(new Cat());
}
public void DoSomething()
{
HelperClass.DoIt(Data);
}
}
class HelperClass {
public static void DoIt(List<Animals> data) {
if (data[0].GetType() == typeof(Cat) ) {
var arrayCats = new Cat[data.Count];
int i = 0;
foreach (Animal item in data) {
arrayCats[i++] = (Cat) item;
}
// use arrayCats
..
}
else if (data[0].GetType() == typeof(Dog) {
// same, then use arrayDogs
..
}
}
}
什么不起作用是直接演员,比如
var arrayCats = (Cat[])data.ToArray();
即使我可以检查所有成员都是猫。有没有更优雅的方法来做到这一点?
您可以通过以下方式执行此操作:
class HelperClass
{
public static void DoIt(List<Animals> data)
{
if(data.All(a => a is Dog))
{
Dog[] dogs = data.OfType<Dog>().ToArray();
// do something with dogs
}
else if(data.All(a => a is Cat))
{
Cat[] cats= data.OfType<Cat>().ToArray();
// do something with cats
}
else
{
throw new ArgumentException("data must contain items of the same type");
}
}
}
使用All
,您可以检查列表中的所有项目是否满足条件(在您的情况下为类型)。OfType()
为您提供具有某种类型的所有项目(在您的情况下:所有项目)自动转换为该类型。如果列表是混合类型,该方法将抛出异常(当然,您可以使用另一种更适合您需要的错误处理)。
但您也应该考虑@AlphaDelta 的评论。对我来说,这个列表只包含一个子类型似乎有点奇怪。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句