[英]In C# 4.0, is it possible to derive a class from a generic type parameter?
I've been trying this, but I can't seem to figure this out.我一直在尝试这个,但我似乎无法弄清楚这一点。 I want to do this...
我想做这个...
public abstract class SingletonType<TSingleton, TBaseClass> : TBaseClass
where TSingleton : TBaseClass, new()
where TBaseClass : class
{
static TSingleton _singleton;
public static TSingleton Singleton
=> _singleton ?? (_singleton = new TSingleton());
}
The plan was to use it like this which would sort of 'wrap' the singleton pattern around a base class...计划是像这样使用它,它将 singleton 模式“包裹”在基础 class 周围......
public class SingletonFoo : SingletonType<SingletonFoo, Foo> {
}
However, I keep getting this但是,我不断收到这个
Cannot derive from 'TBaseClass' because it is a type parameter
无法从“TBaseClass”派生,因为它是类型参数
Um... I thought types were exactly what you do derive from!嗯...我认为类型正是您从中派生的!
So what am I missing?那么我错过了什么?
Note: This is, of course, a trivial example as it doesn't add anything helpful, but assume SingletonType
has a lot of other logic which isn't relative to the question, hence it was omitted to focus on the question at hand.注意:这当然是一个微不足道的例子,因为它没有添加任何有用的东西,但假设
SingletonType
有很多与问题无关的其他逻辑,因此省略了关注手头的问题。
Generic types in C# are not C++ templates; C# 中的泛型类型不是 C++ 模板; remember, a generic type must work for all possible type arguments.
请记住,泛型类型必须适用于所有可能的类型 arguments。 A template need only work for the constructions you actually make.
模板只需要适用于您实际制作的结构。
This question is a duplicate;这个问题是重复的; see my answer to
看我的回答
Why cannot C# generics derive from one of the generic type parameters like they can in C++ templates? 为什么 C# generics 不能像在 C++ 模板中那样从泛型类型参数之一派生?
for more thoughts on this.有关此的更多想法。 Basically, the short answer is that the considerable costs do not outweigh the small benefits of the feature.
基本上,简短的回答是,可观的成本并没有超过该功能的小好处。 If you don't like that answer, see my second answer:
如果您不喜欢该答案,请参阅我的第二个答案:
Why cannot C# generics derive from one of the generic type parameters like they can in C++ templates? 为什么 C# generics 不能像在 C++ 模板中那样从泛型类型参数之一派生?
And if you don't like that answer either, see the follow-up question:如果您也不喜欢这个答案,请参阅后续问题:
What are the good reasons to wish that .NET generics could inherit one of the generic parameter types? 希望 .NET generics 可以继承其中一种通用参数类型的充分理由是什么?
No, this is not possible.不,这是不可能的。 For example, take a type that is declared
sealed
.例如,采用声明为
sealed
的类型。 You can't inherit from that class, and there is no constraint to limit to non sealed types, ergo trying to inherit from it via a generic parameter is impossible.您不能从该 class 继承,并且没有限制非密封类型的限制,因此尝试通过泛型参数从它继承是不可能的。
Every type has exactly 1 real, discrete parent class, even when generics are involved.即使涉及 generics,每种类型都恰好有 1 个真实的离散父 class。 Even when dealing with an open generic type (eg,
typeof(List<>)
), you can still find the parent class.即使在处理开放的泛型类型(例如
typeof(List<>)
)时,您仍然可以找到父 class。
If what you wanted were allowed, this would not be true, typeof(SingletonType<,>
would not have a parent type, and this is not allowed.如果你想要的东西被允许,这不会是真的,
typeof(SingletonType<,>
不会有父类型,这是不允许的。
No, it's not possible, because let's say you have this class:不,这是不可能的,因为假设你有这个 class:
class bar {
int example = 0;
}
Now here's our type parameter class:现在这是我们的类型参数 class:
class foo<T> : T {
int example = 5;
}
If we created a variable of type foo<bar>
then example
would get mixed up.如果我们创建了一个
foo<bar>
类型的变量,那么example
就会混淆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.