And*_*ler 8 c# inheritance interface
我正在尝试从接口继承两个不同的模型。这些模型应该作为列表或集合传递给方法。现在我收到此错误消息:
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
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下我做错了什么吗?: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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
可以在 GitHub 上找到演示项目:https : //github.com/SunboX/InheritanceTest/blob/master/InheritanceTest/Program.cs
作为为什么不能这样做的一个例子,想象一下,除了FooModeland 之外FooModelItem,你还有BarModelItem. 现在让我们说你这样做:
IModel<FooModelItem> fooModel = new FooModel();
IModel<IModelItem> iModel = fooModel;
iModel.Items = new List<BarModelItem>(new BarModelItem());
FooModelItem fooModelItem = fooModel.Items.First();
Run Code Online (Sandbox Code Playgroud)
如果这是有效的代码,您就会遇到麻烦,因为您在最后一行返回的项目实际上不是 aFooModelItem而是BarModelItem!
如果你仔细阅读每一行,你会发现唯一可能出错的行是第二行。这说明了为什么 anIModel<FooModelItem>不能分配给 an IModel<IModelItem>,即使FooModelItem : IModelItem. 无法完成该分配正是您的方法调用失败的原因。
您可以查看通用协方差和逆变,以了解在某些情况下如何避免这种情况,尽管在不修改模型的情况下在您的特定情况下无济于事。
| 归档时间: |
|
| 查看次数: |
42513 次 |
| 最近记录: |