Why does dynamic binding fail when using interface inheritance?

前端 未结 2 842
不知归路
不知归路 2021-01-03 19:30

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);
         


        
相关标签:
2条回答
  • 2021-01-03 20:11

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

    0 讨论(0)
  • It looks like the multiple layers of interface inheritance are doing in the passing of a dynamic type variable. It's definitely tripping up the run time binding.

    At this point if you're looking to get it to work a possible workaround is:

    dynamic x = 23;
    b.Add((object)x);
    
    dynamic y = "Hello, World!";
    b.Add((object)y);
    

    Since dynamic is seen as object by the IL, so casting everything explicitly to type object will get this to work for you.

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