Basically I want to be able to do the following but I think I've designed my class hierarchy wrong or I'm missing something crucial related to polymorphism/generic collections:
ISearchableMediaList<IMedia> list = new MovieList();
The above line produces the following error: Cannot implicitly convert type 'MovieList' to 'ISearchableMediaList<IMedia>'. An explicit conversion exists (are you missing a cast?)
. Shouldn't I be able to substitute the derived type for the interface without casting? (see the class diagram at the bottom for more details).
public interface IMedia
{
string Title { get; set; }
double Rating { get; set; }
DateTime Date { get; set; }
string Comments { get; set; }
}
public class Media : IMedia { ... }
public class Movie : Media { // Adds on movie-specific properties }
public class Book : Media { // Adds on book-specific properties }
public interface IMediaList<T> : ISearchableMediaList<T> where T : IMedia
{ ... }
public interface ISearchableMediaList<T> : IList<T> where T : IMedia
{
IList<string> SearchCategories { get; }
IList<T> Search(string query, string category);
}
public class MediaList<T> : BindingList<T>, IMediaList<T> where T : IMedia
{
// Implements common search functionality and other stuff
}
public class MovieList : MediaList<Movie>
{
// Implements movie-specific searching and other stuff
}
public class BookList : MediaList<Book>
{
// Implements book-specific searching and other stuff
}