C# 未找到覆盖泛型的合适方法 公共类存储库:IDisposable 其中TContext:DbContext,new() { 受保护的TContext上下文; 受保护的存储库() { } 公共虚拟void Create(T项),其中T:class,new() { ... } } 内部类SomeCrud:SomeRepository { 公共覆盖无效创建(产品项) { .... } }
} 我在public override void Create(Product item)上出错,找不到合适的方法进行重写。请有人看到错误?如果我这样写:C# 未找到覆盖泛型的合适方法 公共类存储库:IDisposable 其中TContext:DbContext,new() { 受保护的TContext上下文; 受保护的存储库() { } 公共虚拟void Create(T项),其中T:class,new() { ... } } 内部类SomeCrud:SomeRepository { 公共覆盖无效创建(产品项) { .... } },c#,entity-framework,inheritance,C#,Entity Framework,Inheritance,} 我在public override void Create(Product item)上出错,找不到合适的方法进行重写。请有人看到错误?如果我这样写: public class SomeRepository<TContext> : IDisposable where TContext : DbContext, new() { protected TContext context; protec
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>(Product item)
{
....
}
但是为什么要使用泛型呢?重写和显式接口实现方法的约束是从基方法继承的,因此不能直接指定它们。存储库中的方法工作正常,但继承类中的Create应该是parents的另一个约束。我想我不完全理解您试图实现的目标。您不能以您想要的方式更改重写中的签名。Aphelion look:)我在基类中有用于标准cruds OIOperation的虚拟方法,有时在派生类中我必须更改一些操作。@BeginerDummy好的。在这种情况下,我认为你根本不应该使用泛型。是的,但有些存储库有一个方法,在大多数情况下我不想接触,有时我必须在派生中添加一些指令,这就是为什么我将其签名为虚拟。我正在使用基类中的CRUD来进行以下选择:derivedInstance.DoSome(intid){doing something}存储库模式。我需要基类泛型中的这4个方法。
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) { }
}
public void Create<T>(T item) where T : Product
{
}