C# Bug Fixed InvalidCastException: Cannot cast from source type to destination type.

InvalidCastException: Cannot cast from source type to destination type.

Cannot convert from source type to target type.

The main reason for this problem is that there are errors in type conversion, one of which is not easy to detect is that there are errors when the parent class is cast to a subclass, that is, there may be errors when the transition is down in polymorphism

What is polymorphism: the pointer of the parent class points to the subclass and calls the method of the subclass

First let’s see where my code problem is, for your reference

Obviously, when I was in new tmphead, the pointer of the parent class did not point to the child class at all, so there would be no downward transformation.

We just need to change my code in this way to create a new subclass object..

Read More: