C# 泛型继承的隐式引用转换错误

C# 泛型继承的隐式引用转换错误,c#,generics,inheritance,C#,Generics,Inheritance,我的应用程序中有以下一组类: 这表示一个实体,它是一个SQL表。每个实体类都从该接口继承 public interface IBaseEntity { public int Id { get; set; } } public interface IBaseBLL { //commom methods } 然后我有一个类,它将保存该实体的所有业务逻辑BaseBLL保存所有业务对象通用的方法 public class BLLPai<D>

我的应用程序中有以下一组类:

这表示一个实体,它是一个SQL表。每个实体类都从该接口继承

public interface IBaseEntity
{
     public int Id { get; set; }
}
  public interface IBaseBLL
  {
    //commom methods
  }
然后我有一个类,它将保存该实体的所有业务逻辑
BaseBLL
保存所有业务对象通用的方法

public class BLLPai<D>
        where D : class
{
        protected D EntidadeData;
}
public class BaseBLL<E, D> : BLLPai<D>
        where E: IBaseEntity
        where D: BaseData<E>
{
     //commom methods
}
这是本设计中工作实体(数据库表)的一个示例:

public class MyTable : IBaseEntity
{
    public int Id {get;set;}
    public string Name {get;set;}
}
public MyTableData: BaseData<MyTable>
{
    public List<MyTable> GetAllItems()
    {
        // return result of SELECT
    }
}
public MyTableBLL : BaseBLL<MyTable, MyTableData>
{
    public List<MyTable> GetAllItems()
    {
         // business logic
         return this.EntidadeData.GetAllItems();
    }
}

但是我不想键入数据类,因为它已经包含在BLL类中。我在Vb.Net应用程序中找到了一个类似的设计,它工作得很好。我在这里遗漏了什么导致了隐式引用转换错误?

这里有一个建议。添加一个IBaseDLL接口

public interface IBaseEntity
{
     public int Id { get; set; }
}
  public interface IBaseBLL
  {
    //commom methods
  }
然后将其添加到BaseBLL类的接口列表中

  public class BaseBLL<E, D> : BLLPai<D>, IBaseBLL
          where E : class, IBaseEntity
          where D : BaseData<E>
  {
    //commom methods
  }
公共类BaseBLL:BLLPai,IBaseBLL
其中E:class,IBaseEntity
其中D:基本数据
{
//常用方法
}
现在,您可以将基本控制器更改为以下内容:

[ApiController]
public abstract class GerenciadorLogadaCadastroController<Entidade, BLL> : ControllerBase
        where Entidade : IBaseEntity
        where BLL : IBaseBLL
{
}
[ApiController]
公共抽象类GerenciadorLogadaCadastroController:ControllerBase
实体所在地:Ibseentity
我在哪里
{
}

-Isaac

我对您的代码做了一些简化,将其简化为一个示例,以说明出现了什么问题。有几个步骤

以下是我的初步简化:

public interface IBaseEntity { }
public class MyTable : IBaseEntity { }
public class BaseData<E> where E : IBaseEntity { }
public class MyTableData : BaseData<MyTable> { }
public class BaseBLL<E, D> where E : IBaseEntity where D : BaseData<E> { }
public class MyTableBLL : BaseBLL<MyTable, MyTableData> { }
public abstract class Controller<E, B> where E : IBaseEntity where B : BaseBLL<E, BaseData<E>> { }
public class MyTableController : Controller<MyTable, MyTableBLL> { }
现在我对类型进行了一些巧妙的重命名:

public class Fruit { }
public class Apple : Fruit { }
public class BowlOf<D> where D : Fruit { }
public class BowlOfApples : BowlOf<Apple> { }
public abstract class Controller<B> where B : BowlOf<Fruit> { }
public class BowlOfApplesController : Controller<BowlOfApples> { }
我们的代码可以将香蕉添加到BowlOf!那是不可能的。非法行为是将
保龄球投给
保龄球

简单地说,
BowlOf
不继承自
BowlOf
。尽管苹果公司继承了水果公司的传统,但这一点仍然存在

当您尝试将
BaseBLL
强制转换为
BaseBLL
时,您的代码也犯了同样的错误

在您的问题中,您确实正确地确定了此更改将起作用:

public abstract class Controller<F, B> where F : Apple where B : BowlOf<F> { }
public class BowlOfApplesController : Controller<Apple, BowlOfApples> { }
公共抽象类控制器,其中F:Apple,其中B:BowlOf{}
应用程序控制器的公共类:控制器{}

所以当你说“但是我不想键入数据类,因为它已经包含在BLL类中了。”那么,你不能避免这种情况。

我怀疑你的VB.NET示例是否有效,因为它的类型系统与C#相同。
public class Fruit { }
public class Apple : Fruit { }
public class BowlOf<D> where D : Fruit { }
public class BowlOfApples : BowlOf<Apple> { }
public abstract class Controller<B> where B : BowlOf<Fruit> { }
public class BowlOfApplesController : Controller<BowlOfApples> { }
BowlOf<Apple> apples = new BowlOf<Apple>();
BowlOf<Fruit> fruit = apples; // BOOM!!! imagine if this could be done!
Banana banana = new Banana();
fruit.Add(banana);
public abstract class Controller<F, B> where F : Apple where B : BowlOf<F> { }
public class BowlOfApplesController : Controller<Apple, BowlOfApples> { }