我有一个类层次结构,看起来像这样:类型推论的方法,以仿制药和类继承
class Base<TElement>
{
public TElement Element { get; set; }
}
class Concrete : Base<string>
{
}
我想写接受Base
子类的方法:
public TConcrete DoSomething<TConcrete, TElement>()
where TConcrete : Base<TElement>
{
}
有什么方法可以定义DoSomething,而不必定义TElement
?
理想的解决方案是,如果编译器可以自动计算TElement
,所以调用代码是这样的:
var item = DoSomething<Concrete>();
我使用C#4.0。
'TConcrete'和'TElement'类型与返回或参数类型无关? –
TConcrete是返回类型,我编辑了我的问题 – kshahar
它看起来像你想要[更高级的类型](http://en.wikipedia.org/wiki/Kind_(type_theory)) c#,语言不支持 –