传递动态参数继承自接口调用方法时抛出RuntimeBinderException [英] Passing a dynamic parameter throws RuntimeBinderException when calling Method from Inherited interface

查看:93
本文介绍了传递动态参数继承自接口调用方法时抛出RuntimeBinderException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到了一个有趣的问题,运行时的一些重构后已固定在下面的这个情况。

Ran into an interesting runtime issue after some refactoring and have pinned in down to the following situation.

当传递特性从动态对象的方法在已经从父继承接口运行时粘合剂无法找到该方法的接口。

When passing a property from a dynamic object to a method on an Interface that has been inherited from a parent interface the runtime binder cannot find the method.

下面是一个测试,以展示成功与失败(直接父接口类型调用方法时)

Here is a test to demonstrate both failure and success (when calling method directly on the parent interface type)

using System.Dynamic;
using Microsoft.CSharp.RuntimeBinder;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Test.Utility
{
    public interface IEcho
    {
        string EchoString(string input);
    }

    public interface IInheritEcho : IEcho
    { }

    public class EchoClass : IInheritEcho
    {
        public string EchoString(string input)
        {
            return input;
        }
    }

    [TestClass]
    public class RuntimeBinderTest
    {
        [TestMethod]
        public void RuntimeBinder_should_work_when_dynamic_parameters_are_passed_to_method_from_inherited_interface()
        {
            //Arrange
            dynamic dynObject = new ExpandoObject();
            dynObject.Foo = "Bar";
            IInheritEcho echomore = new EchoClass();

            string echo = null;
            string exceptionMessage = null;

            //Act
            try
            {
                echo = echomore.EchoString(dynObject.Foo);
            }
            catch (RuntimeBinderException e)
            {
                exceptionMessage = e.Message;
            }

            //Assert
            Assert.AreEqual(echo, dynObject.Foo, false, exceptionMessage);
        }

        [TestMethod]
        public void RuntimeBinder_should_work_when_dynamic_parameters_are_passed_to_method_from_noninherited_interface()
        {
            //Arrange
            dynamic dynObject = new ExpandoObject();
            dynObject.Foo = "Bar";
            IEcho echomore = new EchoClass();

            string echo = null;
            string exceptionMessage = null;

            //Act
            try
            {
                echo = echomore.EchoString(dynObject.Foo);
            }
            catch (RuntimeBinderException e)
            {
                exceptionMessage = e.Message;
            }

            //Assert
            Assert.AreEqual(echo, dynObject.Foo, false, exceptionMessage);
        }
    }
}



测试#1失败:
Assert.AreEqual失败。预计:≤(空)>。实际:。 Test.Utility.IInheritEcho'不包含'EchoString的定义

Test #1 Fails: Assert.AreEqual failed. Expected:<(null)>. Actual:. 'Test.Utility.IInheritEcho' does not contain a definition for 'EchoString'

测试#2成功。

我的问题是我的假设,即第一次测试应该通过是否正确,是否有因为它没有框架的根本原因是什么?

My question is whether my assumption that the 1st test should pass is correct or is there a fundamental reason in the framework that it does not?

我知道我可以修复通过转换参数,当我通过他们或通过他们之前将其分配给变量的问题。我更只是好奇,原因继承的接口是造成RuntimeBinder失败...

I know I can fix the issue by casting the parameters when I pass them in or assigning them to variables before passing them in. I'm more just curious as to the reason the inherited interface is causing the RuntimeBinder to fail...

推荐答案

您的情况是对的 Microsoft连接

这篇关于传递动态参数继承自接口调用方法时抛出RuntimeBinderException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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