在 C# 4.0 中,是否可以从泛型类型参数派生类?
我一直在尝试这个,但我似乎无法弄清楚。我想这样做...
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());
}
计划是像这样使用它,这将在基类周围“包装”单例模式...
public class SingletonFoo : SingletonType<SingletonFoo, Foo> {
}
但是,我不断得到这个
无法从“TBaseClass”派生,因为它是类型参数
嗯...我认为类型正是您派生的对象!
那么我错过了什么?
注意:这当然是一个简单的示例,因为它没有添加任何有用的内容,但假设 SingletonType
有很多与问题无关的其他逻辑,因此它被省略以关注手头的问题。
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...
public class SingletonFoo : SingletonType<SingletonFoo, Foo> {
}
However, I keep getting this
Cannot derive from 'TBaseClass' because it is a type parameter
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C# 中的泛型类型不是 C++ 模板;请记住,泛型类型必须适用于所有可能的类型参数。模板只需要适用于您实际制作的结构。
这个问题是重复的; 的回答
请参阅我对为什么 C# 泛型不能像在 C++ 模板中一样从泛型类型参数之一派生?
以获得对此的更多想法。基本上,简短的答案是,相当大的成本不会超过该功能的微小好处。如果您不喜欢这个答案,请参阅我的第二个答案:
为什么 C# 泛型不能像 C++ 模板中那样从泛型类型参数之一派生?
如果您也不喜欢这个答案,请参阅后续问题:
希望 .NET 泛型能够继承其中一种泛型参数类型的充分理由是什么?
Generic types in C# are not C++ templates; remember, a generic type must work for all possible type 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?
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?
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?
不,这是不可能的。例如,采用声明为
sealed
的类型。您不能从该类继承,并且没有限制非密封类型的约束,因此尝试通过泛型参数继承它是不可能的。No, this is not possible. For example, take a type that is declared
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.每种类型都恰好有 1 个真实的、离散的父类,即使涉及泛型也是如此。即使在处理开放泛型类型(例如,
typeof(List<>)
)时,您仍然可以找到父类。如果您想要的内容被允许,则这将是不正确的,
typeof(SingletonType<,>
不会有父类型,这是不允许的。Every type has exactly 1 real, discrete parent class, even when generics are involved. Even when dealing with an open generic type (e.g.,
typeof(List<>)
), you can still find the parent 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.不,这是不可能的,因为假设您有这个类:
现在这是我们的类型参数类:
如果我们创建了一个
foo
类型的变量,那么example
将得到混淆了。No, it's not possible, because let's say you have this class:
Now here's our type parameter class:
If we created a variable of type
foo<bar>
thenexample
would get mixed up.