如何从一个泛型参数继承? [英] How to inherit from a generic parameter?

查看:444
本文介绍了如何从一个泛型参数继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上想做到这一点:

I'm basically wanting to do this:

class UILockable<T> : T
    where T : UIWidget
{
}



不过,这是行不通的。我见过的人建议你这样做:

However, this doesn't work. I've seen people recommend that you do this:

class UILockable<T>
    where T : UIWidget
{
    private T _base;
}

这需要我重写每个功能UILockable需要,并将其转发给T这是不可能的,因为T可从UIWidget派生,并有自己的独特的抽象/虚方法。

This would require me to override each function UILockable would need and forward it to T. This is impossible since T may derive from UIWidget and have unique abstract/virtual methods of its own.

有没有办法简单地在T继承?

Is there no way to simply inherit from T?

推荐答案

您不能从泛型类型参数继承。 C#泛型是从C ++模板非常不同。从类型参数继承需要类基于类型的参数,这是不符合.NET泛型会发生什么有完全不同的表现。他们是在IL和母语水平(所有引用类型参数)相同。

You can't inherit from the generic type parameter. C# generics are very different from C++ templates. Inheriting from the type parameter requires the class to have a completely different representation based on the type parameter, which is not what happens with .NET generics. They are identical at the IL and native level (for all reference type arguments).

这篇关于如何从一个泛型参数继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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