Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# dynamic fails invoking method from a base interface

Tags:

c#

dynamic

Take the following code:

ICanQuack quack = new Duck(); var map = (object) "a map"; quack.Fly((dynamic)map); 

using those types

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!");     } } 

Compiled with C# 5 compiler against .NET 4.5.1 (the behaviour is probably the same using older compiler/framework version) this generates the following error: enter image description here

Now, I have a pretty good idea what is happening under the covers (I blogged about it here) but I can't come up with a satisfying answer why?

like image 250
Krzysztof Kozmic Avatar asked Nov 27 '13 12:11

Krzysztof Kozmic


1 Answers

I am guessing this situation has already been reported to Microsoft.

Take a look here

like image 94
Rohit Avatar answered Sep 25 '22 00:09

Rohit