InvalidCastException: Cannot cast from source type to destination type.

不能从源类型转换为目标类型。

这个问题的原因主要是类型转换出错了,其中一个不易察觉的情况是将父类强制转换为子类的时候出错,也就是在多态时向下转型可能出错

什么是多态:父类的指针指向子类,调用子类的方法

先给大家看一下我的代码问题出在哪里,供与大家参考

C# Bug解决——InvalidCastException: Cannot cast from source type to destination type.

可以明显看到,我在New tmpHead的时候父类的指针根本没有指向子类,所以也不会有向下转型的这个情况 。

我们只需这样更改我的代码,新建的是子类的对象即可。。

C# Bug解决——InvalidCastException: Cannot cast from source type to destination type.

相关文章:

  • 2021-05-10
  • 2021-07-15
  • 2022-12-23
  • 2022-01-27
  • 2022-12-23
  • 2021-08-21
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-15
  • 2022-01-15
  • 2022-12-23
  • 2021-12-22
  • 2021-04-02
相关资源
相似解决方案