【问题标题】:Trying to solve generics error试图解决泛型错误
【发布时间】:2015-07-04 19:22:02
【问题描述】:

我想知道如何启动 BaseDao 类?

public class BaseDao<TEntity, TIdentifier>
where TIdentifier : new()
where TEntity : BaseVo<TIdentifier>
{
//has implementation
}

public class BaseVo<TIdentifier>
    where TIdentifier : new()
{
    /// <summary>
    /// Gets or sets the Identifier.
    /// </summary>
    public virtual TIdentifier Id { get; set; }
}

我正在努力做

 public BaseDao<Department, int>  GetDepartmentRepository()
    {
       return new BaseDao<Department, int>();


    }

但出现错误 - 请帮助 类型“Domain.Models.Entities.Department”不能用作泛型类型或方法“Domain.Models.BaseDao”中的类型参数“TEntity”。没有从“Domain.Models.Entities.Department”到“Domain.Models.BaseVo”的隐式引用转换。

【问题讨论】:

  • 'Department' 是否派生自 'BaseVO
  • 在这种情况下,为了更快地获得帮助,您应该添加 Deparment 类的代码,毕竟这是错误的一部分。

标签: c# oop generics design-patterns


【解决方案1】:

问题是Department类必须是从BaseVo&lt;TIdentifier&gt;派生的。

你可以这样定义部门类:

public class Department : BaseVo<int>
{

}

【讨论】:

  • 谢谢,像魅力一样工作
猜你喜欢
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-01
  • 2015-12-15
  • 1970-01-01
  • 2020-02-20
  • 1970-01-01
相关资源
最近更新 更多