The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Divertimento con il Reflector e un firing method

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
Adrian Florea

Posts: 206
Nickname: adrian11
Registered: Jul, 2004

Adrian Florea is a .NET developer from Italy
Divertimento con il Reflector e un firing method Posted: May 23, 2005 12:46 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Adrian Florea.
Original Post: Divertimento con il Reflector e un firing method
Feed Title: Web Log di Adrian Florea
Feed URL: /error.aspx?aspxerrorpath=/adrian/Rss.aspx
Feed Description: "You know you've achieved perfection in design, not when you have nothing more to add, but when you have nothing more to take away." Antoine de Saint-Exupery
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Adrian Florea
Latest Posts From Web Log di Adrian Florea

Advertisement

Se il seguente snippet:

delegate void FooFiredEvent();
 
class Foo
{
      public event FooFiredEvent FooFired;
     
      public void RaiseFooFired()
      {
            if (FooFired != null)
            {
                  FooFired();
            }
      }
}

lo compiliamo:

csc foo.cs

poi lo disassembliamo :

ildasm foo.exe /out=bar.il

e nel file disassemblato, bar.il, inseriamo la riga in rosso:

.event FooFiredEvent FooFired
{
      .addon instance void Foo::add_FooFired(class FooFiredEvent)
      .removeon instance void Foo::remove_FooFired(class FooFiredEvent)
      .fire instance void Foo::RaiseFooFired()
}

dopodiché lo riassembliamo:

ilasm /exe bar.il

e apriamo il file bar.exe col Reflector impostato per C#, avremo una sorpresa: nel codice disassemblato dell'evento FooFired, è apparsa una sezione, raise, che non c'è ancora nel linguaggio C# :-)

public event FooFiredEvent FooFired
{
      [MethodImpl(MethodImplOptions.Synchronized)] add
      {
            this.FooFired = (FooFiredEvent) Delegate.Combine(this.FooFired, value);
      }
      [MethodImpl(MethodImplOptions.Synchronized)] remove
      {
            this.FooFired = (FooFiredEvent) Delegate.Remove(this.FooFired, value);
      }
      raise
      {
            if (this.FooFired != null)
            {
                  this.FooFired();
            }
      }

}

Vediamo cosa dice Serge Lidin di questi firing methods nel suo libro (sottolineatura mia):

"An event can have at most one firing method. The firing method usually boils down to an invocation of the delegate implementing the event. The Visual C# .NET and Visual Basic .NET compilers, for example, never bother to define a firing method for an event—that is, the method invoking the delegate is there, but it is never associated with the event as a firing method. Such an approach contains a certain logic: the firing method is a purely internal affair of the event publisher and need not be exposed to the event subscribers. And because the compilers, as a rule, use the event metadata to facilitate subscription and unsubscription, associating a firing method with an event is not necessary. If an event does have an associated firing method, however, this method must return void".

Se si imposta il Reflector per altri linguaggi (Visual Basic, Delphi) questa "invenzione" non appare più.

Read: Divertimento con il Reflector e un firing method

Topic: Free project management tools Previous Topic   Next Topic Topic: Digital Work Style: The New World of Work (whitepaper)

Sponsored Links



Google
  Web Artima.com   

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