我想從一個接口繼承兩個不同的模型。應將這些模型作爲列表或集合傳遞給方法。現在我得到這個錯誤信息:類型不能用作泛型類型或方法中的類型參數'T' - 爲什麼?
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'和'FooModelItem' –
Nikhil Agrawal可能是錯誤的,因爲這個問題與你以前的問題完全相同。這裏有一個協變/逆變難題。最好查看關於它們的其他問題,例如[this one](http://stackoverflow.com/q/1228173/395718)。 – Dialecticus