Passing a dynamic parameter throws RuntimeBinderException when calling Method from Inherited interface

前端 未结 2 1941
梦如初夏
梦如初夏 2021-01-12 11:40

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

相关标签:
2条回答
  • 2021-01-12 12:08

    You situations is a documented bug on Microsoft Connect

    0 讨论(0)
  • 2021-01-12 12:14

    A good question this.

    It would appear that it's taking the type of the expression at compile time, IInheritEcho, and not deep-searching the members of inherited interfaces when looking for the method to invoke dynamically.

    And ideally the C# runtime binder for a dynamic expression should behave the same way as the C# compiler - therefore it should see that the IEcho interface is inherited by IInheritEcho and it should work.

    We can test the hypothesis - i.e. that it's the static typing - by doing this in the first test:

    echo = ((dynamic)echomore).EchoString(dynObject.Foo);
    

    Hey presto - the test passes.

    So the issue is not that the dynamic binder can't find the method - it's that when the instance whose member is being invoked dynamically is statically typed as an interface, inherited interfaces are not consulted.

    In my opinion this behaviour is incorrect.

    Please Eric Lippert... be nice...

    0 讨论(0)
提交回复
热议问题