类型不能在泛型类型或方法中用作类型参数“T” - 为什么?

时间:2014-07-02 11:17:27

标签: c# inheritance interface

我正在尝试从接口继承两个不同的模型。这些模型应该作为List或Collection传递给Method。现在我收到此错误消息:

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

1 个答案:

答案 0 :(得分:4)

作为一个为什么你不能这样做的例子,想象一下除了FooModelFooModelItem之外,你还有BarModelItem。现在让我们说你这样做:

IModel<FooModelItem> fooModel = new FooModel();
IModel<IModelItem> iModel = fooModel;
iModel.Items = new List<BarModelItem>(new BarModelItem());

FooModelItem fooModelItem = fooModel.Items.First();

如果这是有效代码,那么您就会遇到麻烦,因为您在最后一行中找回的项目实际上不是FooModelItem而是BarModelItem

如果仔细阅读每一行,您将看到唯一可能的错误行是第二行。这说明了即使IModel<FooModelItem>无法将IModel<IModelItem>分配给FooModelItem : IModelItem的原因。无法执行该分配正是您的方法调用失败的原因。

您可以查看通用协方差和逆变,以了解在某些情况下如何避免这种情况,但如果不修改模型,这对您的特定情况无济于事。