0
我在做这个代码有什么问题?将引用传递给从继承自接口的类继承的类
我有一个接口和一个继承此接口的类。
然后我有另一个类继承该类。
当我试图通过这个类作为参数的方法接受一个List<that interface>
我得到以下错误:
的“射手最佳重载方法匹配(REF System.Collections.Generic.List < ISearchResult>)”具有一些无效参数
参数1:不能从转换 'REF System.Collections.Generic.List' 到 'REF System.Collections.Generic.List < ISearchResult>'
这里是我的接口,类和方法的定义,问题线路编码:
public interface ISearchResult
{
double SearchScore { get; set; }
string SearchIndex { get; set; }
}
public class SearchResult : ISearchResult
{
public double SearchScore { get; set; }
public virtual string SearchIndex { get; set; }
}
public class MyData : SearchResult
{
public int ID { get; set; }
public string Title { get; set; }
public override string SearchIndex { get; set; }
}
public void Scorer(ref List<ISearchResult> results)
{
...
...
List<MyData> myResults = q.ToList();
search.Scorer(ref myResults); //line with the errors
...
自从我使用C#以来这已经有一段时间了,所以这可能已经改变了,但我不认为列表是协变的。你可以让接口使用'IEnumerable'? –
@EvanTrimboli,谢谢,但只是尝试IEnumerable和错误是一样的。 – johna
对我运行正常:https://gist.github.com/evantrimboli/b1d61b51e9a2724066cc375471c046c1 –