我遇到了一个问题,我无法将我的课程转换为其父类型,并且不确定为什么这是考虑最终产品是相同的......是因为课程是抽象的还是由于在构造函数中指定的泛型类型?但是,我会认为对班级的限制会解决这个问题吗?铸造类型的麻烦
对不起,如果我没有解释得很好,但我认为下面的代码可能会更详细地说明问题。
在此先感谢,Onam。
public abstract class ClaimRepository<T> where T : WarrantyClaimBase
{
public ClaimRepository()
{ }
public ClaimRepository(bool IncludeLabour, bool IncludeFranchiseLabour)
{
}
protected abstract void GetFranchiseLabour();
}
public class TestClaimRepository : ClaimRepository<GWMWarrantyClaim>
{
public TestClaimRepository()
{ }
protected override void GetFranchiseLabour()
{
MessageBox.Show("Custom Implementation");
}
}
public sealed class Factory
{
public Factory()
{ }
public ClaimRepository<WarrantyClaimBase> Get()
{
return new TestClaimRepository();
}
}
你正在得到什么确切的错误?它是编译错误还是运行时错误? – 2012-02-03 14:56:16
为什么当你不使用T时,你的类是通用的? – 2012-02-03 14:58:43
我假设'GWMWarrantyClaim'继承自'WarrantyClaimBase'? – Strillo 2012-02-03 14:59:27