简体   繁体   English

抽象/虚拟方法的通用返回类型

[英]Generic return types from abstract/virtual methods

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
}

And their concrete implementations as such: 他们的具体实现如下:

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
            };
  }
}

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 每个MSDN文档http://msdn.microsoft.com/en-us/library/ms379564%28VS.80%29.aspx (大约一半)和一个类似的(但不完全相同)的questoin在SO上指定返回类型根据子类从基类中抽象的方法

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. 我可以将我的concreate实现用于我重写的方法GetCandidates的返回类型,但这不是我想要的,我想利用不同抽象类的具体实现。 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. 我目前得到一个编译时错误,我的GetCandidates返回类型不匹配。

Thanks 谢谢

It looks like you need to define multiple generic types, with one possibly being constrained to derive from CandidateBase. 看起来您需要定义多个泛型类型,其中一个可能被约束为派生自CandidateBase。

Try something like this: 尝试这样的事情:

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
            };
  }
}

Edit Included Chris's correction 编辑包含克里斯的更正

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM