为什么动态绑定失败使用接口继承的时候? [英] Why does dynamic binding fail when using interface inheritance?

查看:170
本文介绍了为什么动态绑定失败使用接口继承的时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,请没有人知道我为什么不能做到以下几点? (特别是行标有不精!下面)

 接口一个
{
无效添加(动态的实体);
}

接口B:A {}

类C:B
{
公共无效添加(动态实体)
{
的System.Console.WriteLine(实体);
}
}

类节目
{
静态无效的主要(字串[] args)
{
B B =新的C();
动态X = 23;
b。增加(23); //罚款
b。增加((int)的X); //罚款
(B为A)。新增(X); //罚款
//b.Add(x); //不精!
}
}



我绝对没有问题,如果呼叫不是动态绑定,或者如果我明确地转换为界面的层次结构的根,但动态绑定调用给我:

 未处理的异常:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:无过载方法'添加'需要在CallSite.Target(封闭,调用点,b,对象)
在System.Dynamic'1'参数
.UpdateDelegates.UpdateAndExecuteVoid2 [T0,T1(调用点现场,T0为arg0,ARG1 T1)
在Program.Main(字串[] args)在C:\Users\Stuart\Documents\Visual Studio 2010中\Projects\CSharp Testbed\Program.cs:行218


解决方案

看微软连接它提起一个错误 - 的动态运行时失败运行


中找到从基接口方法

In C#, please does anyone know why I can't do the following? (specifically the line marked 'NOT fine!' below)

interface A
{
    void Add(dynamic entity);
}

interface B : A {}

class C : B
{
    public void Add(dynamic entity)
    {
        System.Console.WriteLine(entity);
    }
}

class Program
{
    static void Main(string[] args)
    {
        B b = new C();
        dynamic x = 23;
        b.Add(23);        // fine
        b.Add((int)x);    // fine
        (b as A).Add(x);  // fine
        //b.Add(x);       // NOT fine!
    }
}

I have absolutely no problems if the call isn't dynamically-bound, or if I explicitly cast to the interface at the root of the hierarchy, but the dynamically-bound call gives me:

Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: No overload for method 'Add' takes '1' arguments
   at CallSite.Target(Closure , CallSite , B , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[T0,T1](CallSite site, T0 arg0, T1 arg1)
   at Program.Main(String[] args) in C:\Users\Stuart\Documents\Visual Studio 2010\Projects\CSharp Testbed\Program.cs:line 218

解决方案

Looking on Microsoft Connect it's filed as a bug - Dynamic runtime fails to find method from a base interface during runtime

这篇关于为什么动态绑定失败使用接口继承的时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆