Assuming you're using C# 4+ and .NET 4+, you can get this to work using generic covariance:
private static List<Animal> GetFilteredAnimals(string f, Func<IEnumerable<Animal>> method)
A List<Cat>
can't be treated as a List<Animal>
- but it can be treated as an IEnumerable<Animal>
because IEnumerable<T>
is covariant in T
.
Likewise Func<T>
is covariant in T
, so a Func<List<Cat>>
can be treated as a Func<IEnumerable<Animal>>
... so it then compiles.