我试图换了一个两天的节拍下来抽象的方法和回归类型的协方,我已经张贴了两个类似的问题,我永远感激的社区所提供的信息,我只是需要一个最后推到达终点线。这里是我想要做的:2抽象的班级,RecruiterBase和CandidateBase,既有混凝土实现RecruiterA和CandidateA.RecruiterBase有一个抽象的方法,以获得所有新征聘的候选人返回IQueryable.我的执行情况的RecruiterA复盖GetCandidates()法返回IQueryable.

public abstract class RecruiterBase
{ 
  // Constructors declared here

  public abstract IQueryable<CandidateBase> GetCandidates();
}

public abstract class CandidateBase
{  
  // Constructors declared here
}

和实现:

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

public class RecruiterA : RecruiterBase
{
  // 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
            };
  }
}

试图编扔一个编译时间错误的,因为在我的执行情况的RecruitreBase的GetCandidates()方法返回 IQueryable<CandidateA> 而不是的 IQueryable<CandidateBase>.

之后不能够得到的建议从以前的问题(一般的回报类型从抽象/虚拟方法)的工作,我没有更多的读,并且遇到了以下问题在这么

怎么回子类型在复盖方法的类在C#?

这最后使我意识到我一直在寻找一种方法来实现协为我的回归类型。我用马克*Gravell的片段...

abstract class BaseClass
{
    public BaseReturnType PolymorphicMethod()
    { return PolymorphicMethodCore();}

    protected abstract BaseReturnType PolymorphicMethodCore();
}

class DerivedClass : BaseClass
{
    protected override BaseReturnType PolymorphicMethodCore()
    { return PolymorphicMethod(); }

    public new DerivedReturnType PolymorphicMethod()
    { return new DerivedReturnType(); }
}

...作为基础,我的解决方案。所以现在我RecruiterBase和RecruiterA类是这样的:

public abstract class RecruiterBase
{
  // Constructors declared here

  public IQueryable<CandidateBase> GetCandidates()
  {
     return GetCandidatesCore();
  }

  public abstract IQueryable<CandidateBase> GetCandidatesCore();
}

和我的执行情况...

public class RecruiterA : RecruiterBase
{
  // Constructors

  protected override IQueryable<CandidateBase> GetCandidatesCore()
  {
    return GetCandidates();
  }

  public new IQueryable<CandidateA> GetCandidates()
  {
    return from candidates in db.Candidates
           select new CandidateA
           {
             CandidateId = candidates.CandidateId,
             RecruiterId = candidates.RecruiterId
           };
  }
}

我希望这将最终获得我我一直在寻找什么但是我有一个编译错误的时间在以下代码,因为GetCandidates()无法隐含的转换CandidateA到CandidateBase:

  protected override IQueryable<CandidateBase> GetCandidatesCore()
  {
    return GetCandidates();
  }

所以我加了一个演员:

  protected override IQueryable<CandidateBase> GetCandidatesCore()
  {
    return ((IQueryable<CandidateBase>)GetCandidates());
  }

一切,然后汇编而当我实际上叫GetCandidates()在我的控制返回 IQueryable<CandidateBase> 而不是的 IQueryable<CandidateA>.所以我回到我开始。

如果你做了这一切的方式通过这个和你可以帮我我会送你一个12个包你最喜欢的啤酒!

有帮助吗?

解决方案

贾斯汀,我感到有点困惑为什么你需要走过所有的麻烦。

如果你抽象的方法是返回的类型 IQueryable<CandidateBase> 然后你会得到什么。我没有看到这个问题,因为以后你仍然可以转换回来 CandidateACandidateB

那么究竟什么是你想达到什么目的?也许我不了解你的问题。

编辑要添加:

贾斯丁,这是什么?

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

        public abstract IQueryable<CandidateBase> GetCandidates();
    }

    public abstract class CandidateBase
    {
        // Constructors declared here
    }


    public class CandidateA : CandidateBase
    {

    }

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

        // ----HERE IS WHERE I AM BREAKING DOWN----
        public override IQueryable<CandidateBase> GetCandidates()
        {
            return db.Candidates.Where(cand => cand.RecruiterId == this.RecruiterId)
                         .Select(x => new CandidateA
                                          {
                                             CandidateId = c.CandidateId,
                                             CandidateName = c.CandidateName,
                                             RecruiterId = c.RecruiterId
                                           })
                         .Cast<CandidateBase>()
                         .AsQueryable();
        }
    }

其他提示

我认为你的意图是好的,但最终的结果是,你的多晶型代码,也失去了价值。

目的工作对象通过自己的抽象的类型或通过他们的接口是允许你的工作 任何 具体执行,而不需要知道 任何 具体的实施细节。我认为你的信念是,通过返回的具体类型生产更高质量的代码,当事实上你是开始否定价值的抽象基类的复盖的抽象概念。

一个妥当建设的衍生课应该只有很少几个需要加以解决他们的具体类型;抽象类应该足以满足工作,与所有实现和应当处理的绝大多数的工作与这些类--的例外情况应该在少数民族。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top