我正在尝试使用临时接口来获得运行时多重继承,但当我想将对象传递给方法时,我会陷入困境。
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 IWheelsMicrosoft.CSharp.RuntimeBinder.RuntimeBinderException:“MyStuff.PaintWheels(IWheels)”的最佳重载方法匹配有一些无效的参数
我试过试着选InvalidCastException
paintShop.PaintWheels((IWheels)car);System.InvalidCastException :无法将“ImpromptuInterface.ActLikeCaster”类型的对象强制转换为“MyStuff.I轮”。
以下方法可以工作,但我不确定这是正确的方法;在应该继承IWheels接口的情况下,将car转换为IWheels似乎是不合理的:
var wheels = Impromptu.CoerceConvert(car, typeof (IWheels));
paintShop.PaintWheels(wheels);使用临时接口实现运行时多重继承的正确方法是什么?
发布于 2016-05-07 14:04:40
您所遇到的问题都与类型安全性有关--即使在临时使用库时,您也必须确保编译器和运行时确定您要传递给方法的对象是该方法所需的类型。
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();
}
}
}https://stackoverflow.com/questions/37088399
复制相似问题