Extension methods and Curried Delegates


Since Extension methods behave like instance method it makes sense that we should be able to create delegates that would accept the instance method signature, to this end we have included Adding an Extension Methods to delegate invocation List

Extension methods can now be used like an instance methods when being added to a delegate invocation list. So for Extension method Bar defined as

static int Bar(this foo f)


return f.val;


We will now allow delegates that accept the instance method signature to be able to add Extension methods to their invocation list. So for,

delegate int d1();

We can now do

d1 d = f.Bar;

In order to do these the generated IL will look like so

IL_0000: nop

IL_0001: newobj instance void Test.foo::.ctor()

IL_0006: stloc.0

IL_0007: ldloc.0

IL_0008: ldftn int32 Test.Extension::Bar(class Test.foo)

IL_000e: newobj instance void Test.d1::.ctor(object, native int)

Where Test.d1 is the delegate class that we generate. That is I am treating the static method as an instance method and the instance variable is curried to the delegate.

Only Extension methods on reference types are supported for this feature. So you can pass a value type as the instance argument to the extension methods, but as long as its boxed.

I encourage users to try this feature in Beta1 released a few weeks back, and give feedback ...  

kick it on DotNetKicks.com