是否可以通过继承泛型参数来覆盖错误? [英] Is it possible to override the error with inheriting generic parameters?

查看:75
本文介绍了是否可以通过继承泛型参数来覆盖错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过这样的多个问题但是有可能找到这个错误的外部覆盖吗?顺便说一句,错误是:



无法从'T'派生,因为它是一个类型参数


以下是所请求功能的基本示例:

I've seen multiple questions like this but is it possible to find an external override to this error? By the way the error is:

Cannot derive from 'T' because it is a type parameter

Here is a basic example of the requested functionality:

public class Mimic<T> : T //Error
{

}





我以为我可以将通用参数添加到(Project).Properties中,然后检索它以继承。



I was thinking I could add the generic parameter to the (Project).Properties and then retrieve it for the inheritance.

推荐答案

不,你不能。



泛型的全部概念是它们的类型不明确 - 它们是通用的而不是链接到一个特定的类:所以你可以声明一个List类作为一个泛型类的集合,它将与一个整数和一个SqlCommand一样工作。



声明一个类是不同的:编译器需要准确知道基类是什么,以便它可以告诉新类继承的属性,字段,方法和事件 - 以及为了成功继承它需要实现什么来自基类。泛型声明可以这样做,因为它在编译时不知道基类是什么。



如果你能做到这一点,那么你可以做各种非常讨厌的事情,比如创建一个派生自抽象类但没有实现任何抽象方法的类...... X |
No, you can't.

The whole idea of Generics is that they are type unspecific - they are general purpose rather than linked to a specific class: so you can declare a List class that work as a collection of a generic class, and it will work the same with an integer and an SqlCommand.

Declaring a class is different: the compiler needs to know exactly what the base class is so that it can tell what properties, fields, methods and events your new class inherits - and what it needs to implement in order to successfully inherit from the base class. A generic declaration can;t do that, because it doesn't know at compile time what the base class is going to be.

If you could do this, then you could do all sorts of very nasty things, like create a class that derives from an abstract class but which doesn't implement any of its abstract methods... X|


这篇关于是否可以通过继承泛型参数来覆盖错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆