I have a non Abstract Base class Vehicle which has three derived types.
CarMotorcycleTractor
I am looking at a service called VehicleBuilder
Which would have a method CloneVehicle which takes a parameter of Vehicle type and returns a cloned object for all three derived types.
Here's my CloneVehicle
public Vehicle CloneVehicle (Vehicle v)
{
var newVehicle = ClonebaseVehicle(v); // Clones all the types in the Base class
if(Vehicle.Type == Vehicles.Tractor)
{
// Clone individual fields
var tractor = new Tractor();
tractor = newVehicle as Tractor;
tractor.TractorCapacity = 50 ; // tractor is coming null here
return tractor;
}
}
I want to reuse ClonebaseVehicle and cannot create a constructor as the classes are coming from a diferrent DLL.
Any other ways where I can clone the dervied objects using a base class reference ?
I cannot change the Vehicle class or any of its derived classes to create an abstract method in it