泛型奇怪的转换错误

时间:2018-05-20 17:28:40

标签: c# .net generics

我的项目结构是这样的。我有一个界面IUnitOfWork,里面有

 void Commit();
 MyContext Context { get; }
 void Register(BaseRepository<IEntity> repository);

这是我的UnitOfWork类

public class UnitOfWork : IUnitOfWork
{
    private readonly Dictionary<string, BaseRepository<IEntity>> repositories;

    public MyContext Context { get; }

    public UnitOfWork()
    {
        repositories = new Dictionary<string, BaseRepository<IEntity>>();
        this.Context = new MyContext();
    }

    public void Commit()
    {
        repositories.ToList().ForEach(x => x.Value.Submit());
    }

    void IUnitOfWork.Register(BaseRepository<IEntity> repository)
    {
        repositories.Add(repository.GetType().Name, repository);
    }
}

我的IBaseRepository界面

public interface IBaseRepository<T> where T : class, IEntity
{
    List<T> GetAll(Func<T, bool> filter = null);

    void Save(T item);

    void Create(T item);

    void Update(T item, Func<T, bool> findByIDPredecate);
}

我的IEntity界面

public interface IEntity
{
    int Id { get; set; }
}

 public abstract class BaseRepository<TEntity> : IBaseRepository<TEntity>
        where TEntity : class, IEntity
{
    public BaseRepository(IUnitOfWork unitOfWork)
    {
        unitOfWork.Register(this);

但我收到此编译错误

  

错误CS1503参数1:无法转换   'Repositories.BaseRepository<TEntity>'到   'Repositories.BaseRepository<DataAccess.Interfaces.IEntity>'

但是我明确指出TEntity将是IEntity类型(或者它将继承它)。为什么会这样?

0 个答案:

没有答案