As indicated by @IvMisticos, to be able to do pseudo generic polymorphism on open type, you can write:
interface IFoo
{
}
interface IFoo<in T> : IFoo
{
}
class FooCollection
{
List<IFoo> _items;
public FooCollection(List<IFoo> items)
{
_items = items;
}
}
var item1 = some instance of IFoo<int>;
var item2 = some instance of IFoo<double>;
var item3 = some instance of IFoo<string>;
var list = new List<IFoo> { item1, item2, item3 };
var col = new FooCollection(list);
Since there is no diamond operator <>
to allow true generic polymorphism on open types in C#, it is the only thing you can do, as I know.
I really dislike this hack that smells bad.
What exactly is an "open generic type" in .NET?
Generics -Open and closed constructed Types