I'm trying to get runtime multiple inheritance to work using impromptu-interface but I'm stuck when I want to pass the object along to a method.
public interface IEngine {
void Foo();
}
public interface IWheels {
void Foo();
}
public interface IChassie {
void Foo();
}
public interface IPaintShop {
void PaintWheels(IWheels wheels);
void PaintChassie(IChassie chassie);
void ChromeEngine(IEngine engine);
}
var paintShop = Impromptu.ActLike<IPaintShop>();
var car = Impromptu.ActLike(new [] {typeof(IEngine), typeof(IWheels), typeof(IChassie) } );
// dynamic car = Impromptu.ActLike(new [] {typeof(IEngine), typeof(IWheels), typeof(IChassie) } ); // Same error
paintShop.PaintWheels(car); // RuntimeException as car is dynamic and not the expected IWheels
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : The best overloaded method match for 'MyStuff.PaintWheels(IWheels)' has some invalid arguments
I've tried to cast but get InvalidCastException
:
paintShop.PaintWheels((IWheels)car);
System.InvalidCastException : Unable to cast object of type 'ImpromptuInterface.ActLikeCaster' to type 'MyStuff.IWheels'.
The following works but I'm not sure this is the correct way; it seem unwarranted to convert car to IWheels
when IWheels
interface should already be inherited:
var wheels = Impromptu.CoerceConvert(car, typeof (IWheels));
paintShop.PaintWheels(wheels);
What is the correct way of achieving runtime multiple inheritance using impromptu-interface?