繁体   English   中英

如何解决这个错误? 方差无效:类型参数“T”必须是无效的

[英]How to fix this error? Invalid variance: The type parameter 'T' must be invariantly valid on

我在编译时遇到以下错误消息:

“无效的方差:类型参数'T'必须在'ConsoleApplication1.IRepository.GetAll()'上无效。”T'是协变的。“

以下是我的代码:

 class Program
{

    static void Main(string[] args)
    {
        IRepository<BaseClass> repository;

        repository = new RepositoryDerived1<Derived1>();

        Console.ReadLine();
    }
}

public abstract class BaseClass
{

}

public class Derived1 : BaseClass
{

}

public interface IRepository<out T> where T: BaseClass, new()
{
    IList<T> GetAll();
}

public class Derived2 : BaseClass
{

}

public abstract class RepositoryBase<T> : IRepository<T> where T: BaseClass, new()
{
    public abstract IList<T> GetAll();
}

public class RepositoryDerived1<T> : RepositoryBase<T> where T: BaseClass, new()
{
    public override IList<T> GetAll()
    {
        throw new NotImplementedException();
    }
}

我需要的是能够像这样使用上面的类:

IRepository存储库;

要么

RepositoryBase存储库;

然后我希望能够分配这样的东西:

repository = new RepositoryDe​​rived1();

但是它给IRepository类带来了编译时错误。

如果我从IRepository类中删除“out”关键字,它会给我另一个错误

“RepositoryDe​​rived1”无法转换为“IRepository”。

为什么以及如何解决它?

谢谢

IList<T>不是协变的。 如果将IList<T>更改为IEnumerable<T> ,并从IRepository<out T>删除: new()约束(因为抽象基类不满足),它将起作用:

public interface IRepository<out T> where T : BaseClass
{
    IEnumerable<T> GetAll();
}

public abstract class RepositoryBase<T> : IRepository<T> where T : BaseClass, new()
{
    public abstract IEnumerable<T> GetAll();
}

public class RepositoryDerived1<T> : RepositoryBase<T> where T : BaseClass, new()
{
    public override IEnumerable<T> GetAll()
    {
        throw new NotImplementedException();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM