简体   繁体   English

在 C# 4.0 中,是否可以从泛型类型参数派生 class?

[英]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.

相关问题 "C# - 我可以从具有通用基类型的类派生吗?" - C# - can I derive from class with generic base type? 是否可以将C#泛型方法类型参数约束为“可从”包含类的类型参数“赋值”? - Is it possible to constrain a C# generic method type parameter as “assignable from” the containing class' type parameter? 在C#类中隐含一个泛型类型参数 - Implying one generic type parameter from another in C# class 来自子类的C#泛型类型参数,可能吗? - C# generic type argument from child class, possible? C# 泛型 class 与泛型参数相同的类型参数和约束 - C# generic class type parameter and constraint that is the same as the generic parameter C# 如何从抽象泛型 class 继承 class 并使用泛型 class 作为类型参数 - C# How to inherit class from abstract generic class with generic class as type parameter c#获取泛型类中泛型类型参数的名称 - c# get the name of the generic type parameter in generic class 是否可以在C#中使用未绑定类型作为泛型类型参数? - Is it possible to use an unbound type as a generic type parameter in C#? 来自类的C#4.0通用报告生成器 - C# 4.0 Generic report generator from a class 使用嵌套在泛型类中的类作为C#中的类型参数 - Using a class nested in a generic class as type parameter in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM