简体   繁体   English

从抽象泛型类继承的c#抽象泛型类

[英]c# Abstract Generic Class inheriting from Abstract Generic Class

I'm trying to create an abstract generic class which inherits from another abstract generic class. 我正在尝试创建一个继承自另一个抽象泛型类的抽象泛型类。

Here's what I have so far 这是我到目前为止的

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
    }
}

The TryGetFallback method on the DerivedDerivedClass causes a compiler error. DerivedDerivedClass上的TryGetFallback方法导致编译器错误。

First you need to fix your BaseClass<T> implementation to not have a recursive type constraint. 首先,您需要修复BaseClass<T>实现,使其不具有递归类型约束。

public abstract class BaseClass<T> : BaseClass where T : new() {
    //snip
}

Then you can use it in your derived class, for example I will make it use int for the generic type parameter: 然后,您可以在派生类中使用它,例如,我将使int用于泛型类型参数:

public abstract class DerivedClass : BaseClass<int> {
    //snip
}

And now if you compile it will warn you that 'DerivedDerivedClass' does not implement inherited abstract member 'BaseClass<int>.TryGetFallback(long)' 现在,如果您进行编译,它将警告您'DerivedDerivedClass' does not implement inherited abstract member 'BaseClass<int>.TryGetFallback(long)'

Thanks for the tips @DavidG it's helped me to solve the problem with the following code 感谢@DavidG的提示,它通过以下代码帮助我解决了问题

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<T>, new() {
    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; // Try to get item from cache here
        if (item != null) { return true; }
        else {
            T obj = new T();
            item = obj.TryGetFallback(id);
            return item != null;
        }
    }

    protected abstract T TryGetFallback(long id);
}

public abstract class DerivedClass<T> : BaseClass<T> where T : DerivedClass<T>, new() {
    public String Name { get; private set; }

    public DerivedClass() : base(0) {  }

    public DerivedClass(long id, String name)
        : base(id) {
        this.Name = name;
    }

    protected abstract override T TryGetFallback(long id);
}

public class DerivedDerivedClass : DerivedClass<DerivedDerivedClass> {

    public DerivedDerivedClass() {

    }

    protected override DerivedDerivedClass TryGetFallback(long id) {
        throw new NotImplementedException();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM