Я пытаюсь создать абстрактный общий класс, который наследуется от другого абстрактного обобщенного класса.C# Абстрактный универсальный класс, наследующий от абстрактного обобщенного класса
Вот что я до сих пор
public abstract class BaseClass {
public long Id { get; private set; }
public BaseClass(long id) {
this.Id = id;
}
}
public abstract class BaseClass<T> : BaseClass where T : BaseClass {
protected BaseClass(long id)
: base(id) {
}
public static T Get(long id) {
T item;
return TryGet(id, out item) ? item : default(T);
}
public static bool TryGet(long id, out T item) {
item = null; // This is where I call the cache but for this example I've removed so it will compile
if (item != null) { return true; }
else {
// Call TryGetFallback method
return false;
}
}
protected abstract T TryGetFallback(long id);
}
public abstract class DerivedClass : BaseClass<DerivedClass> {
public String Name { get; private set; }
public DerivedClass(long id, String name)
: base(id) {
this.Name = name;
}
}
public class DerivedDerivedClass : DerivedClass {
protected override DerivedDerivedClass TryGetFallback(long id) {
// Handle the try get fallback
}
}
Метод TryGetFallback на DerivedDerivedClass вызывает ошибку компилятора.
DerivedDerivedClass наследует от DerivedClass, который наследует от DerivedClass, который наследуется от BaseClass. В вашей цепочке нет BaseClass , поэтому ваш метод не в цепочке. –
kha
'BaseClass' нигде не используется! –
DavidG
Также вы уверены, что ограничение типа для BaseClass '' должно быть 'BaseClass ', кажется немного ... рекурсивным! –
DavidG