sách gpt4 ai đã đi

Kiểu trả về chung cho các phương thức trừu tượng/ảo

In lại 作者:行者123 更新时间:2023-11-30 17:24:12 29 4
mua khóa gpt4 Nike

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

public abstract class RecruiterBase
{
// Properties declare here
// Constructors declared here

public abstract IQueryable GetCandidates();
}

public abstract class CandidateBase
{
// Properties declare here
// 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 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/en-us/library/ms379564%28VS.80%29.aspx (大约下降了一半)以及关于 SO 的类似(但不相同)问题 Specifying the return type of an abstract method from a Base Class according to a Sub Class

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

Cảm ơn

1 Câu trả lời

看起来您需要定义多个泛型类型,其中一个可能被限制为从 CandidateBase 派生。

尝试这样的事情:

public abstract class RecruiterBase where C : CandidateBase
{
// Properties declare here
// Constructors declared here

public abstract IQueryable GetCandidates();
}

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

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

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

biên tập包括克里斯的更正

关于c# - 抽象/虚拟方法的通用返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1330473/

29 4 0
Bài viết được đề xuất: c# - IHTMLTxtRange.pasteHTML 不会替换旧的 HTML
Bài viết được đề xuất: c# - ASP.NET 中组件的许可证
Bài viết được đề xuất: c# - 如何检测用户何时使用 .NET (C#) 登录系统?
Bài viết được đề xuất: Javascript - 如何检查输入的图像 URL 是否确实存在
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com