我有一系列类型的集合,所有这些都派生自同一个基类,以及一组用于搜索每个类型的谓词,例如
public abstract class Animal { ... }
public class Dog : Animal { ... }
public class Cat : Animal { ... }
...
Func<Dog, bool> DogFinder = ...;
Func<Cat, bool> CatFinder = ...;
...
List<Dog> Dogs = GetDogs(DogFinder);
List<Cat> Cats = GetCats(CatFinder);
有没有办法可以避免每种类型的重复?
我的下一步是采用 Dogs、Cats 并转换为常见的“结果”类型并返回这些相当简单的集合,但我觉得中间的重复应该被排除在外,以便我添加更多类型的动物未来它将干净地扩展。