在一些重构之后进入一个有趣的运行时问题,并将其归结为以下情况.
将属性从动态对象传递到已从父接口继承的接口上的方法时,运行时绑定程序无法找到该方法.
这是一个测试,用于演示失败和成功(直接在父接口类型上调用方法)
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); …Run Code Online (Sandbox Code Playgroud)