I'm trying to add a few concrete instances into collection where all the concrete instances inherit from the same interface. But it doesn't compile.
// Common interface.
public interface ISearchService<in TSearchOptions> where TSearchOptions : ISearchOptions
{ .. }
// Concrete #1.
public class FooSearchService : ISearchService<FooSearchOptions>
{ .. }
// Concrete #2.
public class BaaSearchService : ISearchService<BaaSearchOptions>
{ .. }
So with the above code, both Foo
and Baa
both inherit from an ISearchService<ISearchOptions>
but each one has defined their own specific SearchOptions type.
So then I'm trying this (which doesn't compile) :
var searchServices = new List<ISearchService<ISearchOptions>>
{
new FooSearchService(),
new BaaSearchService()
};
I've written the full code up on .NET Fiddle.
The idea is that I have a common collection of concrete instances which they all have a common base interface.
I'm pretty sure that I'm failing at this because of some *variance sillyness which I just don't understand.
I thought maybe I'm trying to do too much .. so I've tried a new .NET Fiddle with something different but that also doesn't work.
I just want a mixed bag of a common generic interface.