I did this little console application to illustrate my problem.
I wonder if there is a way to access the properties on the class which is the current implementation of the interface you are working on. In this example below I would like to set the vehicle interface property Engine if it is a car. I found a way to do this using the brackets when you are doing a new instance of car, but I would like to do this later by doing vehicle.Engine = "something";
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var car = (Car)GetVehicle(VehicleType.Car);
var bike = (Bike)GetVehicle(VehicleType.Bike);
Console.ReadKey();
}
static IVehicle GetVehicle(VehicleType type)
{
IVehicle vehicle = null;
switch (type)
{
case VehicleType.Car:
vehicle = new Car() { Engine = "V8", Name = "Car Name" };//Works
vehicle.Engine = "cannot do this"; //This does not work
break;
case VehicleType.Bike:
vehicle = new Bike() { Mountainbike = true, Name = "Bike name" }; // Works
vehicle.Mountaikbike = true; //This does not work either, same problem
break;
default:
break;
}
return vehicle;
}
}
public enum VehicleType
{
Car, Bike
}
public class Car : IVehicle
{
public string Name { get; set; }
public string Engine { get; set; }
}
public class Bike : IVehicle
{
public string Name { get; set; }
public bool Mountainbike { get; set; }
}
public interface IVehicle
{
string Name { get; set; }
}
}