我试图实现使用泛型(C#/ 3.5) 一个辅助方法,我有类一个不错的结构,基类,像这样:在一个通用的功能使用的基础对象作为参数
public class SomeNiceObject : ObjectBase
{
public string Field1{ get; set; }
}
public class CollectionBase<ObjectBase>()
{
public bool ReadAllFromDatabase();
}
public class SomeNiceObjectCollection : CollectionBase<SomeNiceObject>
{
}
我希望使用中检索收集像普通的方法,以便:
public class DAL
{
public SomeNiceObjectCollection Read()
{
return ReadFromDB<SomeNiceObjectCollection>();
}
T ReadFromDB<T>() where T : CollectionBase<ObjectBase>, new()
{
T col = new T();
col.ReadAllFromDatabase();
return col;
}
}
这不建,与
Error 66 The type 'SomeNiceObjectCollection' cannot be used as type parameter 'T' in the generic type or method 'ReadFromDB<T>'. There is no implicit reference conversion from 'SomeNiceObjectCollection' to 'CollectionBase<ObjectBase>'.
SomeNiceObjectCollection对象是一个CollectionBase,是一个精确的CollectionBase。那么我如何才能使这个工作?
Excellent.CollectionBase媒体链接实现的,我需要一个接口,所以我更改了ReadFromDB。谢谢! –
edosoft
2009-09-11 12:52:13