Here is a sample code showing the oddity: (code fixed based on comment)
public class C{}
public static class E {
public static void Foo(this C o) { }
}
class D:C {
void test() {
// Foo(); // will not compile
this.Foo(); // compile ok
}
}
naturally in a real scenario class C would be a class i do not have access to its source code
Anyone knows why this odd requirement to use the this keyword?
Looking at the C# spec, section 7.6.5.2 specifies that extension method invocations only apply for method calls of the following formats:
When it is a standalone method call, there is no
expr, so it does not look for an extension method. This follows because without anexpr, it doesn’t know what the first argument of the extension method should be.