找不到覆盖泛型的合适方法

时间:2012-06-19 11:22:31

标签: c# entity-framework inheritance

public class SomeRepository<TContext> : IDisposable
            where TContext : DbContext, new()
        {
            protected TContext context;
            protected SomeRepository()
            { }

        public virtual void Create<T>(T item) where T : class, new()
        {
            ...
        }
    }

    internal class SomeCrud : SomeRepository<SomeContext>
    {
        public override void Create(Product item)
        {
            ....
        }     
    }

}

我在公共覆盖上遇到错误void Create(Product item)找不到合适的方法覆盖。请问有人看到错误吗?如果我这样写:

        public override void Create<Product>(Product item)
        {
            ....
        }

我看不到产品类型 感谢

1 个答案:

答案 0 :(得分:2)

我认为您正在寻找这个解决方案:

public class SomeRepository<TContext, T> where TContext : DbContext where T : class, new()
{
    public virtual void Create(T item) { }
}

internal class SomeCrud : SomeRepository<SomeContext, Product>
{
    public override void Create(Product item) { }
}

您实际上应该在通用定义中定义产品的约束。 请注意T

中的SomeRepository<TContext, T>

你可以尝试

    public void Create<T>(T item) where T : Product
    {           
    }

但为什么然后使用泛型?