Is it possible to cast an object from a base to a derived one? For instance, let’s say I have one base object: Animal. From this I have derived a couple of other classes: Cat, Dog.
Now I need an Animal but the type at this moment doesn’t really matter. So,
Animal blankCanvas = new Animal();
Along the way I changed my mind and now want to change that Animal to a Dog. However, I don’t understand how I would go about doing that. I know the business rules to convert from an Animal to a Dog but I just don’t know how to implement that.
There is no way to do this without writing a custom conversion. Probably a better thing to do would be to decide much earlier what kind of derived type that you need and create an instance of that.
Most of the time when the type system is going to fight you in this way you need to redesign your architecture.