我搜索了网络和stackoverflow,发现了许多处理CS0311错误的帖子。没有一个场景接近我的。我有一个继承自泛型类的泛型类。
请注意,BusinessBase
是CSLA
框架
我错过了什么?
public interface Itest
{
int Digit();
}
class BB : Itest
{
public int Digit()
{
return 20;
}
}
class Test<T> : BusinessBase<T>, Itest where T : Test<T>, Itest
{
public int Digit()
{
return 30;
}
}
Test<Itest> test = new Test<Itest>(); //error CS0311
错误CS0311类型
'MyTestApp.Itest'
不能用作泛型类型或方法'T'
中的类型参数'A<T>'
。没有从'MyTestApp.Itest'
到'MyTestApp.A<MyTestApp.Itest>'
的隐式引用转换。 MyTestApp
答案 0 :(得分:4)
我同意编译器。您的方案中T
为Itest
。您的where
条件要求T : Test<T>
(这听起来很可疑),这需要ITest : Test<ITest>
...但ITest
显然不会t (并且不能): Test<ITest>
。
我们非常不清楚你打算做什么,但我怀疑你的意思是:
class Test<T> where T : Itest
和
Test<BB> test = new Test<BB>();
答案 1 :(得分:1)
你可以这样:
interface Itest {}
class BusinessBase<T> {
}
class Test<T> : BusinessBase<T>, Itest where T : Test<T>, Itest {
public int Digit() {
return 30;
}
}
class IT : Test<IT>, Itest {
}
class Program {
public static int Main() {
var t = new Test<IT>();
t.Digit();
return 0;
}
}
对我而言,对泛型的使用非常糟糕,但这就是CSLA的工作原理....