I have generic container class I'm using that I'm trying to write a OfType routine for. However the types I'm passing in are also generics. Here's an example:
Entities.OfType<Foo<Bar>>()
and my function definition:
public IEnumerable T OfType<T>()
{
foreach (var e in Values)
if (e is T)
yield return (T)e;
}
If Entities are defined as a collection of Foo<Base>
I get an error Foo<Bar>
does not inherit from Foo<Base>
. However Bar does inherit from Base.
Is there a way around this limitation?