我想要这样的东西
public class GenericClass<T1> where T1 : class, new()
{
public void GenericMethod<T2>(GenericClass<T2> t)
{
//do my stuff...
}
}
我遇到了这样的编译错误
类型“ T2”必须是引用类型,才能在通用类型或方法“ Rextester.GenericClass”中用作参数“ T1”
“ T2”必须是具有公共无参数构造函数的非抽象类型,以便在通用类型或方法“ GenericClass”中将其用作参数“ T1”
类型T1和T2没有类型关系。 有可能吗?
答案 0 :(得分:1)
对不起,我的错
public class GenericClass<T1> where T1: class, new()
{
// <-- I got an error here because I should add same constraint for T2 as it will be used in GenericClass
public void GenericMethod<T2>(GenericClass<T2> t)
// The following line will work fine
public void GenericMethod<T2>(GenericClass<T2> t) where T2: class, new()
{
//do my stuff...
}
}