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

发布于 2024-08-02 10:02:29 字数 1482 浏览 4 评论 0原文

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

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

Per MSDN 文档 http://msdn.microsoft.com/en- us/library/ms379564%28VS.80%29.aspx (大约向下一半) 以及一个类似(但不相同)的问题 指定返回类型根据子类从基类中提取抽象方法

我可以利用我的具体实现来实现我的重写方法 GetCandidates 的返回类型,但这不是我想要的,我想利用具体的不同抽象类的实现。这是父/子数据库关系。我想要实现的目标可能吗?我当前收到一个编译时错误,表明我的 GetCandidates 返回类型不匹配。

谢谢

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

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.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒尘 2024-08-09 10:02:29

看起来您需要定义多个泛型类型,其中一种可能被限制为从 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 looks like you need to define multiple generic types, with one possibly being constrained to derive from 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文