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:
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?