I want to manipulate an instance by a delegate, which is a property of the class itself. The parameter of the delegate should always be the instance itself (even in derived classes!).
See code below. I know that the code is not compiling because I have to cast car1 to type car, I'm looking for a solution without casting.
Code
static void Main(string[] args)
{
var car = new Car();
car.VehicleManipulator = car1 => car1.SomeInt++;
car.ManipulateVehicle();
Console.WriteLine("end");
Console.ReadLine();
}
internal class Vehicle
{
public Action<Vehicle> VehicleManipulator { get; set; }
public void ManipulateVehicle()
{
this.VehicleManipulator(this);
}
}
internal class Car : Vehicle
{
public int SomeInt { get; set; }
}
EDIT: Changed Code!
My question is, is there a good solution to handle all this just in the base class, but in the action I want to use the derived classes without casting.