共用方式為


Extension methods and Curried Delegates

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

Comments

  • Anonymous
    May 08, 2007
    You've been kicked (a good thing) - Trackback from DotNetKicks.com

  • Anonymous
    May 13, 2007
    Welcome to the 27th Community Convergence. I use this column to keep you informed of events in the C#

  • Anonymous
    May 29, 2007
    Overview C# 3.0 introduces many new constructs and opens entirely new ways of thinking and developing

  • Anonymous
    February 27, 2008
    Sree wrote down some more details on extension methods. The standard functionality is quite easy to use,

  • Anonymous
    July 11, 2008
    Celexa lexapro dose. Heavy drinking celexa. Celexa. Common withdrawal effects of celexa. Celexa side effects.

  • Anonymous
    July 18, 2008
    Ultracet. Is ultracet addictive. Ultracet addiction.

  • Anonymous
    April 08, 2009
    I am not able to understand what are you trying to tell. Can you elaborate little bit?

  • Anonymous
    March 18, 2011
    I suffer from Fibromyalgia. Although prescribed 8 tablets daily, I typically take 4 if I can get away with it. It's very effective for pain. http://www.genericultracet.com