在C#泛型继承? [英] generic inheritance in C#?

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

问题描述


可能重复:结果
为什么不能C#泛型从泛型类型参数之一获得像他们可以在C ++模板?






我可以做

 公共类MyGenericClass:DL 
//但我不能做
公共类MyGenericClass< T> :T已



我会怎么做第二次?如果我不能做到这一点,我怎么可以这样做。



 公共类MyGenericClass< T> 
{$ B $(B T)OBJ;
//都MyGenericClass.XYZ调用obj.XYZ
}


解决方案

这是不可能的,因为这取决于什么类型的T是,MyGenericClass的公共接口将改变。



如果你有大量的不同类别,所有暴露相同的界面,你可以声明MyGenericClass揭露该接口,并在所有的函数的实现委托调用OBJ


Possible Duplicate:
Why cannot C# generics derive from one of the generic type parameters like they can in C++ templates?

I can do

public class MyGenericClass : DL
//but i cannot do
public class MyGenericClass <T> : T

How would i do the second? if i cannot do that, how can i do something like

public class MyGenericClass <T> 
{
    T obj;
    //have all MyGenericClass.XYZ call obj.XYZ
}

解决方案

This is not possible, because depending on what type T is, the public interface of MyGenericClass would change.

If you have lots of different classes that all expose the same interface, you could declare MyGenericClass to expose that interface, and in the implementation of all of the functions delegate the calls to obj

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

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