泛型 错误 无法将类型 “class1” 转换为“class1

ghao0 2012-04-18 03:39:40
我的函数
public void Myfunction(class1<typeBase> 参数)
{
...
}
希望传入参数的类型class1<type1>
type1继承于typeBase
如何做到?

如果直接传会报错!
class1<type1> 参数1;
....

Myfunction(参数1);

编译报错
泛型 错误 无法将类型 “class1<type1>” 转换为“class1<typeBase>”
...全文
274 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
ghao0 2012-04-18
  • 打赏
  • 举报
回复
谢谢大家参与,我准备绕弯子了。
也就是说:
泛型类是不变的。也就是说,如果输入参数指定 List<BaseClass>,则当您试图提供 List<DerivedClass> 时,将会发生编译时错误。
EnForGrass 2012-04-18
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 的回复:]

List<class1<type1>> thelist = ...
List<class1<typeBase>> list = new List<class1<typeBase>>();
foreach (class<type1> item in thelist)
{
list.Add(item as class<typeBase>);
}
---------------
it……
[/Quote]
既然是as 你觉得他会是什么
因为list是泛型List<class1<typeBase>> 当然需要转换
既然要返回List<typeBase>把type1转换成typeBase即可
ghao0 2012-04-18
  • 打赏
  • 举报
回复
public class Class1<T> where T : typeBase
{
....
}
语义上有一个基本类Class1<typeBase>但是实际上不是如此吗?
ghao0 2012-04-18
  • 打赏
  • 举报
回复
List<class1<type1>> thelist = ...
List<class1<typeBase>> list = new List<class1<typeBase>>();
foreach (class<type1> item in thelist)
{
list.Add(item as class<typeBase>);
}
---------------
item as class<typeBase> 会返回什么?如果成功,应该用他就可以了吧
铜臂阿铁木 2012-04-18
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 的回复:]

//this.typeBaseList不仅包含Class1<type1>,还包含Class1<type2>???不可能???
[/Quote]

可能的。。。
ghao0 2012-04-18
  • 打赏
  • 举报
回复
//this.typeBaseList不仅包含Class1<type1>,还包含Class1<type2>???不可能???
threenewbee 2012-04-18
  • 打赏
  • 举报
回复
为class1<typeBase>写一个显式的类型转换操作符:

public static explicit operator class1<typeBase>(class1<T> x)
{
return new class1<typeBase>(x); //假设class1<typeBase>有这么一个构造函数。
}

再用我11楼的代码

List<class1<type1>> thelist = ...
List<class1<typeBase>> list = new List<class1<typeBase>>();
foreach (class<type1> item in thelist)
{
list.Add(item as class<typeBase>);
}
铜臂阿铁木 2012-04-18
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]

引用 14 楼 的回复:
引用 7 楼 的回复:

引用 5 楼 的回复:
引用 3 楼 的回复:

引用 1 楼 的回复:
4.0以下的貌似做不到,4.0或其以上的可以通过定义<in T>实现

4.0也做不到。


必须做不到。

因为class1<type1>和class1<typeBase>没有任何关系。


做不到的原因不是这个,是因为C# 4.0只……
[/Quote]

但是List<Type>没有继承List<typeBase>啊

如果typeBase有两个子类 Type 和Type2, 就可以看出问题所在了呗。
一个 List<typeBase> baselist= new List<typeBase>();
baseClass.Add(new Type1()); baseClass.Add(new Type2());
这让baseList怎么往List<Type>上联系啊。

所以我的理解是,即使Type与typeBase有继承关系,但是这不说明List<Type>与List<typeBase>有继承关系啊。
ghao0 2012-04-18
  • 打赏
  • 举报
回复
--刚刚的回复有问题
public void Myfunction<T>(class1<T> 参数) where T : typeBase
{
...
}
不行,在函数内部进行对比时出现同样问题
函数内部,我引用了一个List<Class1<typeBase>>类型,typeBaseList
typeBaseList的函数不能调用参数!
具体调用为
if(!this.typeBaseList.Contains(参数))....
我用的是2005

如何实现?
!this.typeBaseList.Contains(参数)
//this.typeBaseList不仅包含Class1<type1>,还包含Class1<type2>
ghao0 2012-04-18
  • 打赏
  • 举报
回复
---刚刚回复的有问题,现更正
public void Myfunction<T>(class1<T> 参数) where T : typeBase
{
...
}
不行,在函数内部进行对比时出现同样问题
函数内部,我引用了一个List<class1<typeBase>>类型,BaseList
BaseList的函数不能调用参数!
具体调用为
if(!this.BaseList.Contains(参数))....
我用的是2005

如何实现?
!this.BaseList.Contains(参数)
//this.BaseList不仅包含Class1<type1>,还包含Class1<type2>


threenewbee 2012-04-18
  • 打赏
  • 举报
回复
用我11楼的代码转化下。
ghao0 2012-04-18
  • 打赏
  • 举报
回复
public void Myfunction<T>(class1<T> 参数) where T : typeBase
{
...
}
不行,在函数内部进行对比时出现同样问题
函数内部,我引用了一个List<typeBase>类型,typeBaseList
typeBaseList的函数不能调用参数!
具体调用为
if(!this.typeBaseList.Contains(参数))....
我用的是2005

如何实现?
!this.typeBaseList.Contains(参数)
//this.typeBaseList不仅包含type1,还包含type2
threenewbee 2012-04-18
  • 打赏
  • 举报
回复
哦,看错了。

不需要的。只要这个函数泛型就可以了。
ghao0 2012-04-18
  • 打赏
  • 举报
回复
晕呀!
我不能把Myfunction<T>函数所在的Class也做成泛型 ,因为不可能呀
threenewbee 2012-04-18
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 的回复:]
引用 7 楼 的回复:

引用 5 楼 的回复:
引用 3 楼 的回复:

引用 1 楼 的回复:
4.0以下的貌似做不到,4.0或其以上的可以通过定义<in T>实现

4.0也做不到。


必须做不到。

因为class1<type1>和class1<typeBase>没有任何关系。


做不到的原因不是这个,是因为C# 4.0只能支持泛型和委托的协变,不支持类……
[/Quote]
lz说了“type1继承于typeBase”,你没看到。
铜臂阿铁木 2012-04-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

引用 5 楼 的回复:
引用 3 楼 的回复:

引用 1 楼 的回复:
4.0以下的貌似做不到,4.0或其以上的可以通过定义<in T>实现

4.0也做不到。


必须做不到。

因为class1<type1>和class1<typeBase>没有任何关系。


做不到的原因不是这个,是因为C# 4.0只能支持泛型和委托的协变,不支持类。
[/Quote]

用协变来说的话,我感觉应该这么解释:class<type>和class1<typeBase>也根本没有关系啊,两种类型。
就像ClassA和ClassB一样。

转换什么的,还是像版主那么转换吧。
最好写个T ConvertTo<T,S>(S in)这类的
threenewbee 2012-04-18
  • 打赏
  • 举报
回复
只能这样。
ghao0 2012-04-18
  • 打赏
  • 举报
回复
不会要求把
Myfunction<T>函数所在的Class也做成泛型吧
threenewbee 2012-04-18
  • 打赏
  • 举报
回复
List<type1> thelist = ...
List<typeBase> list = new List<typeBase>();
foreach (type1 item in thelist)
{
list.Add(item as typeBase);
}
...
ghao0 2012-04-18
  • 打赏
  • 举报
回复
public void Myfunction<T>(class1<T> 参数) where T : typeBase
{
...
}
不行,在函数内部进行对比时出现同样问题
函数内部,我引用了一个List<typeBase>类型,typeBaseList
typeBaseList的函数不能调用参数!
具体调用为
if(!this.typeBaseList.Contains(参数))....
我用的是2005
加载更多回复(9)

110,569

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧