子类参数问题 'no suitable method found to override'
Problem with subclass parameter 'no suitable method found to override'
我有一个具有以下方法的 DataSourcesController:
public abstract ActionResult<IEnumerable<DataSource>> Add(DataSourceDTO dataSourceDTO);
public abstract ActionResult<IEnumerable<DataSource>> Edit(DataSourceDTO dataSourceDTO);
我有一个覆盖这些方法的 SQLDataSourcesController:
[HttpPost]
public override ActionResult<IEnumerable<DataSource>> Add(SQLDataSourceDTO sqlDataSourceDTO)
{ //code is in here }
[HttpPut]
public override ActionResult<IEnumerable<DataSource>> Edit(SQLDataSourceDTO sqlDataSourceDTO)
{ //code is in here }
但是,由于 SQLDataSourceDTO 而不是 DataSourceDTO,我收到以下错误:
SQLDataSourcesController.Add(SQLDataSourceDTO': no suitable method found to override
SQLDataSourcesController.Edit(SQLDataSourceDTO': no suitable method found to override
我的 SQLDataSourceDTO 是 DataSourceDTO 的子类但是:
public abstract class DataSourceDTO
{ //code is in here }
public class SQLDataSourceDTO : DataSourceDTO
{ //code is in here }
感谢任何有关如何解决此问题的帮助!
您不能使用派生类型作为参数覆盖该方法,因为签名不匹配。对于覆盖,签名应该完全匹配。
但您可以改用泛型类型。
public class Base
{
public int Id
{
get;
set;
}
}
public class Derived : Base
{
public string Name
{
get;
set;
}
}
public class Controller<T> where T : Base, new()
{
public virtual void Compute(T b)
{
}
}
public class SqlController : Controller<Derived>
{
public override void Compute(Derived b)
{
}
}
我有一个具有以下方法的 DataSourcesController:
public abstract ActionResult<IEnumerable<DataSource>> Add(DataSourceDTO dataSourceDTO);
public abstract ActionResult<IEnumerable<DataSource>> Edit(DataSourceDTO dataSourceDTO);
我有一个覆盖这些方法的 SQLDataSourcesController:
[HttpPost]
public override ActionResult<IEnumerable<DataSource>> Add(SQLDataSourceDTO sqlDataSourceDTO)
{ //code is in here }
[HttpPut]
public override ActionResult<IEnumerable<DataSource>> Edit(SQLDataSourceDTO sqlDataSourceDTO)
{ //code is in here }
但是,由于 SQLDataSourceDTO 而不是 DataSourceDTO,我收到以下错误:
SQLDataSourcesController.Add(SQLDataSourceDTO': no suitable method found to override
SQLDataSourcesController.Edit(SQLDataSourceDTO': no suitable method found to override
我的 SQLDataSourceDTO 是 DataSourceDTO 的子类但是:
public abstract class DataSourceDTO
{ //code is in here }
public class SQLDataSourceDTO : DataSourceDTO
{ //code is in here }
感谢任何有关如何解决此问题的帮助!
您不能使用派生类型作为参数覆盖该方法,因为签名不匹配。对于覆盖,签名应该完全匹配。
但您可以改用泛型类型。
public class Base
{
public int Id
{
get;
set;
}
}
public class Derived : Base
{
public string Name
{
get;
set;
}
}
public class Controller<T> where T : Base, new()
{
public virtual void Compute(T b)
{
}
}
public class SqlController : Controller<Derived>
{
public override void Compute(Derived b)
{
}
}