While trying to write elegant answer to another SO question I stumbled upon strange issue on extension methods.
Consider the following setup:
public class Foo
{
}
public static class Extensions
{
public static string ExtensionTest(this object o)
{
return "object overload called";
}
public static string ExtensionTest(this Foo f)
{
return "Foo overload called";
}
}
When I execute following:
object o = new Foo();
Console.WriteLine(o.ExtensionTest());
It outputs object overload called
(instead of Foo overload called
)
- Why not dynamic binding?
- Is there any way to "force" dynamic binding on extension methods (without messy
if-else
s orswitch
ing types)?