1
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)
        {
            ....
        }     
    }

}

我在 public override void Create(Product item) not suitable method found to override 时遇到错误。请有人看到错误吗?如果我这样写:

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

我看不到产品类型 谢谢

4

1 回答 1

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) { }
}

您实际上应该在通用定义中定义产品的约束。注意TSomeRepository<TContext, T>


你可以试试

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

但是为什么还要使用泛型呢?

于 2012-06-19T11:25:35.100 回答