无法从List< Bar>转换列出< Foo> [英] Cannot convert from List<Bar> to List<Foo>
本文介绍了无法从List< Bar>转换列出< Foo>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的设置:
abstract class Foo {}
class Bar : Foo {}
以及这种形式的其他方法:
and a method elsewhere of this form:
void AddEntries(List<Foo>) {}
我试图使用类型为Bar
List<Bar> barList = new List<Bar>()
AddEntries(barList);
但这给了我错误:
无法从List< Bar>转换列出< Foo>
cannot convert from List<Bar> to List<Foo>
反正这个问题存在吗?我需要使用抽象类保留方法定义.
Is there anyway around this problem? I Need to keep the method definition using the abstract class.
推荐答案
您可以将您的AddEntries
泛型化并将其更改为此
You could make your AddEntries
generic and change it to this
void AddEntries<T>(List<T> test) where T : Foo
{
//your logic
}
看看有关类型参数的约束以获取更多信息.
这篇关于无法从List< Bar>转换列出< Foo>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文