I have a class that have a property (SearchResults
) that need to be List<T>
, where T
is one of my search classes depend on a condition
public class SearchGeneralResponse : ActionResponse
{
public IList<ISearchRecord> SearchResults { get; set; }
public SearchGeneralResponse(MbsObjectType searchType)
{
if(searchType == MbsObjectType.SourceRepo) SearchResults = new List<SearchRecord>();
if(searchType == MbsObjectType.BuildConfiguration) SearchResults = new List<SearchRecordBuild>();
}
}
I can cast new SearchRecord
to ISearchRecord
. But when I do it with list
this.SearchResults = new List<SearchRecord>();
I get this error:
Cannot implicitly convert type System.Collections.Generic.List 'SearchRecord' to System.Collections.Generic.List 'ISearchRecord'
Here's my interface:
public interface ISearchRecord
{
}
And one of the derived classes:
public class SearchRecord : ISearchRecord
{
public string Name { get; set; }
public string Summary { get; set; }
}
How can I create a List<T>
property that can be initialized to a list of a class depending on a certain condition?