泛型 错误 无法将类型 “class1<type1>” 转换为“class1<typeBase>”
我的函数
public void Myfunction(class1<typeBase> 参数)
{
...
}
希望传入参数的类型class1<type1>
type1继承于typeBase
如何做到?
如果直接传会报错!
class1<type1> 参数1;
....
Myfunction(参数1);
编译报错
泛型 错误 无法将类型 “class1<type1>” 转换为“class1<typeBase>”
[解决办法]
4.0以下的貌似做不到,4.0或其以上的可以通过定义<in T>实现
[解决办法]
public void Myfunction(class1<typeBase> 参数)
{
...
}
=>
public void Myfunction<T>(class1<T> 参数) where T : typeBase
{
...
}
[解决办法]
[解决办法]
把class1<type1>整体当做一个类了,应该是
public void Myfunction<T>(T myclass)
限定T是typeBase
[解决办法]
[解决办法]
[解决办法]
List<type1> thelist = ...
List<typeBase> list = thelist.Select(x => x as typeBase).ToList();
[解决办法]