【发布时间】:2009-07-10 09:14:34
【问题描述】:
我在下面的指定行中收到错误“T 不包含 Id 的定义”,即使在调试时,我看到“item”确实 在其基类中有一个属性“Id”。
如何在此处指定我希望 C# 在项目的基类中查找 Id(为什么它不自动执行此操作?)?
//public abstract class Items<T> : ItemBase (causes same error)
public abstract class Items<T> where T : ItemBase
{
public List<T> _collection = new List<T>();
public List<T> Collection
{
get
{
return _collection;
}
}
public int GetNextId()
{
int highestId = 0;
foreach (T item in _collection)
{
//ERROR: "T does not contain a definition for Id
if (item.Id > highestId) highestId = item.Id;
}
return highestId;
}
}
这是定义类的方式:
public class SmartForm : Item
{
public string IdCode { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int LabelWidth { get; set; }
public int FormWidth { get; set; }
...
public abstract class Item : ItemBase
{
public int Id { get; set; }
public DateTime WhenCreated { get; set; }
public string ItemOwner { get; set; }
public string PublishStatus { get; set; }
public int CorrectionOfId { get; set; }
...
【问题讨论】:
-
ItemBase中Id的定义是什么?听起来像是私人的
-
ItemBase的定义是什么?
标签: c# generics inheritance