【发布时间】:2016-01-07 03:49:53
【问题描述】:
我不确定这是否可能,希望得到一些澄清。
我有一个这样的类结构:
public class FooBase
{
//Some base class
}
public class BarBase
{
//Some base class
}
public class Foo : FooBase
{
//Implementation
}
public class Bar : BarBase
{
//Implementation
}
public abstract class FooBarHolderAbstract<T, V> where T: FooBase where V: BarBase
{
}
public class MyFooBarHolderImpl : FooBarHolderAbstract<Foo, Bar>
{
}
public class FooBarTest
{
public void DoSomethingWithFooBar<T>() where T : FooBarHolderAbstract<FooBase, BarBase>
{
//Do something tith the obj
}
public void RunTest()
{
//This doesn't work, compiler says MyFooBarHolder is not convertible to FooBarHolderAbstract<FooBase, BarBase>
DoSomethingWithFooBar<MyFooBarHolderImpl>();
}
}
在 FooBarTest 类中,我想创建一个接受泛型参数的方法,该方法继承自具有两个泛型参数的抽象类。 MyFooBarHolderImpl 类扩展了抽象基类,并使用继承自抽象类的泛型参数类型的类型指定其泛型参数。
当我尝试调用此方法 (DoSomethingWithFooBar()) 时,编译器告诉我 MyFooBarHolderImpl 类型必须可转换为 FooBarHolderAbstract
这是根本无法完成的事情,还是我缺少概念/语法?
提前致谢!
【问题讨论】:
-
@thebigbo:你在两个方面都错了。也许您正在考虑另一种语言?
-
大哥,你在这两个方面都错了,这不是 c++
-
是的。对困惑感到抱歉。我以为是c++
标签: c# .net generics inheritance abstract