【发布时间】:2014-07-02 11:17:27
【问题描述】:
我正在尝试从一个接口继承两个不同的模型。这些模型应该作为列表或集合传递给方法。现在我收到此错误消息:
The type 'InheritanceTest.FooModel' cannot be used as type parameter 'T' in the generic type or method 'InheritanceTest.Service.DoSomethingWith<T>(System.Collections.Generic.IEnumerable<T>)'. There is no implicit reference conversion from 'InheritanceTest.FooModel' to 'InheritanceTest.IModel<InheritanceTest.IModelItem>'. C:\Work\InheritanceTest\InheritanceTest\Program.cs 14 13 InheritanceTest
有人可以解释一下,我做错了什么吗? :D
演示代码:
interface IModel<T> where T : IModelItem
{
string Name { get; set; }
IEnumerable<T> Items { get; set; }
}
interface IModelItem
{
string Name { get; set; }
}
class FooModel : IModel<FooModelItem>
{
public FooModel()
{
Items = new List<FooModelItem>();
}
public string Name { get; set; }
public IEnumerable<FooModelItem> Items { get; set; }
}
class FooModelItem : IModelItem
{
public string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
var fooLists = new List<FooModel>();
var barLists = new ObservableCollection<BarModel>();
var service = new Service();
service.DoSomethingWith(fooLists);
service.DoSomethingWith(barLists);
}
}
class Service
{
public void DoSomethingWith<T>(IEnumerable<T> list) where T : IModel<IModelItem>
{
foreach (var model in list)
{
Debug.WriteLine(model.Name);
foreach (var item in model.Items)
{
Debug.WriteLine(item.Name);
}
}
}
}
演示项目可以在 GitHub 上找到: https://github.com/SunboX/InheritanceTest/blob/master/InheritanceTest/Program.cs
【问题讨论】:
-
那么
FooModelItem和IModelItem是什么关系?我相信class FooModel : IModel<IModelItem>可以实现您想要实现的目标 -
更新了代码以包含
IModelItem和FooModelItem -
Nikhil Agrawal 可能错误地认为这个问题与您之前的问题完全相同。在这里,您遇到了协方差/逆变难题。最好查看有关它们的其他问题,例如 this one。
标签: c# inheritance interface