1

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?

4

1 回答 1

1

您遇到的问题都与类型安全有关——即使使用像 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();
        }
    }
}
于 2016-05-07T14:04:40.463 回答