简体   繁体   English

泛型基类,其中 T 也是泛型基类

[英]Generic base class where T is also a generic base class

I have a generic base class something like:我有一个通用基类,例如:

public abstract class SomeThingBase<T> where T : class

I'd like to have another base class something like this:我想要另一个像这样的基类:

public abstract class MangerBase<T> where T : SomeThingBase<T>

How do you accomplish this in C#?你如何在 C# 中实现这一点? When trying to use ManagerBase like this it doesn't seem to work.当尝试像这样使用 ManagerBase 时,它​​似乎不起作用。

class TestManager : ManagerBase<TestSomething>

In order to do this you have to pass your generic declarations in all the way from the top.为了做到这一点,您必须从顶部一直传递您的泛型声明。 This can get very messy, so avoid doing it.这可能会变得非常混乱,因此请避免这样做。

public abstract class SomeThingBase<T> where T : class{ }
public abstract class ManagerBase<T, U> 
    where T : SomeThingBase<U>
    where U : class
{ }

It's definitely possible to define, but impossible to actually implement.绝对可以定义,但无法实际实施。 Assuming you have the below definition:假设您有以下定义:

    public abstract class SomeThingBase<T> where T : class { }
    public abstract class MangerBase<T> where T : SomeThingBase<T> { }

However, defining a class that implements ManagerBase<T> and SomethingBase<T> is impossible since multiple inheritance is not supported and T cannot meet both constraints without multiple inheritance the generic type.但是,定义一个实现ManagerBase<T>SomethingBase<T>是不可能的,因为不支持多重继承,并且T不能在没有多重继承泛型类型的情况下满足这两个约束。

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

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