[英]Why I can not call base constructor method with an argument?
public class GenericRepository<TEntity> where TEntity : class
{
internal DbContext context;
internal DbSet<TEntity> dbSet;
public GenericRepository(DbContext context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
//snip
}
public class MyRepository<TEntity> where TEntity : GenericRepository<TEntity>
{
public MyRepository(DbContext context) : base(context){ }
//snip
}
I extended the GenericRepository
class, and to use base's member variables I need to call Base's constructor in child's constructor. 我扩展了
GenericRepository
类,并使用base的成员变量,我需要在子的构造函数中调用Base的构造函数。 But I got an error that says: 但我得到一个错误,上面写着:
'object' does not contain a constructor that takes 1 arguments
'object'不包含带有1个参数的构造函数
Even though the GenericRepository
has constructor. 即使
GenericRepository
有构造函数。
What am I doing wrong? 我究竟做错了什么?
Because your "base class" is object
, not GenericRepository<TEntity>
. 因为您的“基类”是
object
,而不是GenericRepository<TEntity>
。 You added a constraint on TEntity
, you did not inherit from GenericRepository<TEntity>
. 您在
TEntity
上添加了约束,您没有从GenericRepository<TEntity>
继承。 Maybe you meant this: 也许你这意味着:
public class MyRepository<TEntity> : GenericRepository<TEntity> where TEntity : class
{
public MyRepository(DbContext context) : base(context){ }
You have to change MyRepository
's base class to GenericRepository<TEntity>
Also you need to leave where TEntity : class
restriction 你必须将
MyRepository
的基类更改为GenericRepository<TEntity>
你还需要离开where TEntity : class
restriction
public class MyRepository<TEntity> : GenericRepository<TEntity> where TEntity : class
{
public MyRepository(object context)
:base(context)
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.