【发布时间】:2011-05-01 22:01:28
【问题描述】:
我有以下课程
public class AccountingBase<TItemType> where TItemType : AccountingItemBase
在我的 AccountingItemBase 中,我有以下属性:
public virtual AccountingBase<AccountingItemBase> Parent { get; set; }
在我的 AccountingBase 中,我正在尝试执行以下操作
item.Parent = this;
从逻辑上讲,这应该可以工作,因为 TItemType 继承自 AccountingItemBase,但我收到以下错误:
> Error 1 Cannot implicitly convert type
> 'TGS.MySQL.DataBaseObjects.AccountingBase<TItemType>'
> to
> 'TGS.MySQL.DataBaseObjects.AccountingBase<TGS.MySQL.DataBaseObjects.AccountingItemBase>'
如何将子属性的父属性设置为自身(在父类内部)
【问题讨论】:
-
愚蠢的问题,我可以看到
TItemType的声明(包括命名空间)吗? -
@drachenstern:愚蠢的问题,哪个? OP还是你的? :->
-
@drachenstern:
TItemType是一个类型参数。它没有单独的声明。 -
@hermeister der welten ~ 我的。
-
有人在 /facepalm 日标记我……我有一种感觉,我会一遍又一遍地做这件事……一遍又一遍……我的咖啡呢?
标签: c# generics inheritance polymorphism