我試圖實現使用泛型(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