我试图使用即兴接口让运行时多重继承发挥作用,但当我想把对象传递给方法时,我遇到了困难。
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:最佳的重载方法匹配"MyStuff.PaintWheels(IWheels)"有一些无效的参数
我试过投,但得到了InvalidCastException
:
paintShop.PaintWheels((IWheels)car);
System.InvalidCastException:无法强制转换类型为的对象要键入的"ImpromptuInterface.ActLikeMaster"我的东西。高跟鞋"。
以下方法有效,但我不确定这是正确的方法;当IWheels
接口应该已经被继承时,将car转换为IWheels
似乎是没有必要的:
var wheels = Impromptu.CoerceConvert(car, typeof (IWheels));
paintShop.PaintWheels(wheels);
使用即兴接口实现运行时多重继承的正确方法是什么?
您遇到的问题都与类型安全有关——即使在使用Impromptu这样的库时,您也必须确保编译器和运行时确保您传递到方法中的对象是方法所需的类型。
ActLike<T>
可以实现许多接口,但它只返回T
的单个类型化实例,因此如果没有一个类型告诉编译器您的实例实现了多个接口,您将被迫强制转换为必要的接口。
此外,ImpromptuInterface允许您用一个与该对象的实现非正式匹配的接口来包装对象,即使该接口没有正式声明。作为该库的使用者,您仍然需要提供要包装的库的实现。
尝试以下操作:
using System;
using ImpromptuInterface;
using ImpromptuInterface.Dynamic;
namespace Example
{
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);
}
internal class Program
{
public static void Main(string[] args)
{
var ps = new
{
PaintWheels = ReturnVoid.Arguments<IWheels>(wheels => wheels.Foo()),
PaintChassie = ReturnVoid.Arguments<IChassie>(chassie => chassie.Foo()),
ChromeEngine = ReturnVoid.Arguments<IEngine>(engine => engine.Foo())
};
var paintShop = ps.ActLike<IPaintShop>();
var fullCar = new
{
Foo = ReturnVoid.Arguments(() => Console.WriteLine("Hello World!"))
};
var car = fullCar.ActLike<IEngine>(typeof(IChassie),typeof(IWheels));
//each of these 3 calls prints "Hello World!" to the console
paintShop.PaintWheels((IWheels)car);//need to tell the compiler to cast your car to type IWheels because var car is of type IEngine
paintShop.PaintChassie(car as IChassie);//need to tell the compiler to cast your car to type IChassie because var car is of type IEngine
paintShop.ChromeEngine(car);//works sans cast because var car is of type IEngine
//each of these 3 calls prints "Hello World!" to the console, too
dynamic dynamicCar = car;
paintShop.PaintWheels(dynamicCar);//by using dynamic you disable the compile time
paintShop.PaintChassie(dynamicCar);//type checking and the compiler "trusts you" on the typing
paintShop.ChromeEngine(dynamicCar);//since Impromptu wrapped your object and implemented the interfaces for you, there is no runtime exception
Console.ReadLine();
}
}
}