Search code examples
c#reflectioncopy-constructorderived-class

Derived and base class, can I set the base explicitly?


public class SuperCar: Car
{
     public bool SuperWheels { get {return true; } }
}

public class Car 
{
     public bool HasSteeringWheel { get {return true;} }
}

How can I set the base class for the derived Supercar?

For example, I want to simply set SuperCars base class like this:

public void SetCar( Car car )
{
SuperCar scar = new SuperCar();
car.Base = car; 
}

Basically, if I have Car objects, I do not want to manually iterate through every property of the car in order to setup the SuperCar oject, which I think is the only way you can do it but if you can do it the other way it would be sooo much better.


Solution

  • Overall a lot of helpful comments. I think the short answer was given by pst and I think this is correct:

    No. Short reason: There is no separate base object. (object)this == (object)base is always true. There are ways to perform cloning/copying by reflection (and other means) though. Perhaps describe what is really wanted

    So, his suggestion of using the automapper tool was also incredibly useful and was basically what I was looking for.