抽象/虚拟方法的通用返回类型 [英] Generic return types from abstract/virtual methods

查看:72
本文介绍了抽象/虚拟方法的通用返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两个基类之间有关系:

I have a relationship between two base classes:

public abstract class RecruiterBase<T>
{
  // Properties declare here
  // Constructors declared here

  public abstract IQueryable<T> GetCandidates();
}

public abstract class CandidateBase<T>
{
  // Properties declare here
  // Constructors declared here
}

及其具体实现方式:

public class CandidateA : CandidateBase<CandidateA>
{
  // Constructors declared here
}

public class RecruiterA : RecruiterBase<RecruiterA>
{
  // Constructors declared here

  // ----HERE IS WHERE I AM BREAKING DOWN----
  public override IQueryable<CandidateA> GetCandidates()
  {
     return from c in db.Candidates
            where c.RecruiterId == this.RecruiterId
            select new CandidateA
            {
              CandidateId = c.CandidateId,
              CandidateName = c.CandidateName,
              RecruiterId = c.RecruiterId
            };
  }
}

每个MSDN文档 http://msdn.microsoft.com/zh-我们/图书馆/ms379564%28VS.80%29.aspx (大约下降了一半) 并在SO上进行类似(但不完全相同)的问题 指定返回类型从基类到子类的抽象方法的说明

Per MSDN documentation http://msdn.microsoft.com/en-us/library/ms379564%28VS.80%29.aspx (about half way down) and a similiar (but not identical) questoin on SO Specifying the return type of an abstract method from a Base Class according to a Sub Class

我可以将concreate实现用于重写的方法GetCandidates的返回类型,但这不是我想要的,我想利用其他抽象类的具体实现.这是父/子数据库关系.我试图实现的目标有可能吗?我目前收到一个GetCandidates返回类型不匹配的编译时错误.

I can make use of my concreate implementation for the return type of my overridden method GetCandidates but that is not what I want, I want to make use of the concrete implementation of a different abstract class. This is a parent/child database relationship. Is what I am trying to achieve possible? I currently get a compile time error that my GetCandidates return type does not match.

谢谢

推荐答案

您似乎需要定义多个泛型类型,其中一种类型可能会受到限制,不能从CandidateBase派生.

It looks like you need to define multiple generic types, with one possibly being constrained to derive from CandidateBase.

尝试这样的事情:

public abstract class RecruiterBase<T, C> where C : CandidateBase
{
  // Properties declare here
  // Constructors declared here

  public abstract IQueryable<C> GetCandidates();
}

public abstract class CandidateBase<T>
{
  // Properties declare here
  // Constructors declared here
}

public class CandidateA : CandidateBase<CandidateA>
{
  // Constructors declared here
}

public class RecruiterA : RecruiterBase<RecruiterA, CandidateA>
{
  public override IQueryable<CandidateA> GetCandidates()
  {
     return from c in db.Candidates
            where c.RecruiterId == this.RecruiterId
            select new CandidateA
            {
              CandidateId = c.CandidateId,
              CandidateName = c.CandidateName,
              RecruiterId = c.RecruiterId
            };
  }
}

编辑包括克里斯的更正

这篇关于抽象/虚拟方法的通用返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆