The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Re: Generics, Anonymous Methods, & Delegate inference

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Udi Dahan

Posts: 882
Nickname: udidahan
Registered: Nov, 2003

Udi Dahan is The Software Simplist
Re: Generics, Anonymous Methods, & Delegate inference Posted: Aug 23, 2006 2:32 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Udi Dahan.
Original Post: Re: Generics, Anonymous Methods, & Delegate inference
Feed Title: Udi Dahan - The Software Simplist
Feed URL: http://feeds.feedburner.com/UdiDahan-TheSoftwareSimplist
Feed Description: I am a software simplist. I make this beast of architecting, analysing, designing, developing, testing, managing, deploying software systems simple. This blog is about how I do it.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Udi Dahan
Latest Posts From Udi Dahan - The Software Simplist

Advertisement
In my previous post .Net 2.0 Generics, Anonymous Methods, and Delegate inference bug I bemoaned the difference in behavior between anonymous methods and delegates.

When using plain old event handlers, it is perfectly acceptable to subscribe and unsubscribe by doing the following:

Commands.Open.Activated += new EventHandler<OpenEventArgs>(Open_Activated);
Commands.Open.Activated -= new EventHandler<OpenEventArgs>(Open_Activated);

Even though the delegate that you are removing is a different object than the one you subscribed with, since delegates are immutable they behave like value types so this works. However, with anonymous delegates this is apparently not the case (as my previous post showed). The following code is incorrect, even for the same object 'f':

Commands.Open.Activated += this.GetOpenPolylineCallbackFor(f);
Commands.Open.Activated -= this.GetOpenPolylineCallbackFor(f);

Considering that anonymous methods are syntactic sugar for regular delegates, and the whole variable promotion is handled by the compiler, it is unclear why anonymous methods behave differently.

Anyway, here's the correct code - courtesy of Ayende Rahien:

private void RouteForm_OnNeedPolyline(object sender, EventArgs e)
{
  IRouteForm f = sender as IRouteForm;
  if (f == null) return;

  EventHandler<OpenEventArgs> openCallback = delegate(object sender, OpenEventArgs e)
  {
    Polyline p = e.Entity as Polyline;
    if (p != null)
      f.Polyline = p;

    Commands.Open.Activated -= openCallback;
  };

  Commands.Open.Activated += openCallback;

  Commands.New.Activate(this, new NewEventArgs(typeof(Polyline)));
}

Read: Re: Generics, Anonymous Methods, & Delegate inference

Topic: Reduce the weight of stylesheets by 35% at runtime Previous Topic   Next Topic Topic: [Blog] Sprechen ist Gold, Schweigen ist...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use