我有以下接口是现有项目的一部分.我想用动态对象调用Store(..)函数.但我不想更改接口层次结构(如果可能的话).
public interface IActualInterface
{
void Store(object entity);
}
public interface IExtendedInterface : IActualInterface
{
//Interface items not important
}
public class Test : IExtendedInterface
{
public void Store(object entity)
{
Console.WriteLine("Storing: " + entity.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
和以下代码:
IExtendedInterface extendedInterfaceTest = new Test();
IActualInterface actualInterfaceTest = new Test();
Test directTest = new Test();
dynamic employee = new ExpandoObject();
employee.Name = "John Smith";
employee.Age = 33;
employee.Phones = new ExpandoObject();
employee.Phones.Home = "0111 123123";
employee.Phones.Office = "027 321123";
employee.Tags = new …Run Code Online (Sandbox Code Playgroud) 请使用以下代码:
ICanQuack quack = new Duck();
var map = (object) "a map";
quack.Fly((dynamic)map);
Run Code Online (Sandbox Code Playgroud)
使用这些类型
public interface ICanFly
{
void Fly<T>(T map);
}
public interface ICanQuack : ICanFly
{
void Quack();
}
public class Duck : ICanQuack
{
public void Fly<T>(T map)
{
Console.WriteLine("Flying using a {0} map ({1})", typeof (T).Name, map);
}
public void Quack()
{
Console.WriteLine("Quack Quack!");
}
}
Run Code Online (Sandbox Code Playgroud)
使用针对.NET 4.5.1的C#5编译器编译(使用较旧的编译器/框架版本的行为可能相同),这会生成以下错误:

现在,我非常清楚在幕后发生了什么(我在这里写了博客)但我无法想出一个令人满意的答案为什么?
在一些重构之后进入一个有趣的运行时问题,并将其归结为以下情况.
将属性从动态对象传递到已从父接口继承的接口上的方法时,运行时绑定程序无法找到该方法.
这是一个测试,用于演示失败和成功(直接在父接口类型上调用方法)
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)