首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有临时接口的运行时多重继承

具有临时接口的运行时多重继承
EN

Stack Overflow用户
提问于 2016-05-07 12:14:11
回答 1查看 489关注 0票数 1

我正在尝试使用临时接口来获得运行时多重继承,但当我想将对象传递给方法时,我会陷入困境。

代码语言:javascript
复制
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

代码语言:javascript
复制
paintShop.PaintWheels((IWheels)car);

System.InvalidCastException :无法将“ImpromptuInterface.ActLikeCaster”类型的对象强制转换为“MyStuff.I轮”。

以下方法可以工作,但我不确定这是正确的方法;在应该继承IWheels接口的情况下,将car转换为IWheels似乎是不合理的:

代码语言:javascript
复制
var wheels = Impromptu.CoerceConvert(car, typeof (IWheels));
paintShop.PaintWheels(wheels);

使用临时接口实现运行时多重继承的正确方法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-07 14:04:40

您所遇到的问题都与类型安全性有关--即使在临时使用库时,您也必须确保编译器和运行时确定您要传递给方法的对象是该方法所需的类型。

ActLike<T>可以实现许多接口,但是它只返回一个T的类型化实例,因此如果没有一个类型告诉编译器您的实例实现了几个接口,那么您将被迫转换到必要的接口。

此外,ImpromptuInterface允许您用一个接口包装对象,该接口与该对象的实现非正式地匹配,即使接口没有正式声明。作为该库的使用者,您仍然必须为库提供要包装的实现。

尝试如下所示:

代码语言:javascript
复制
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();
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37088399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档