Click here to Skip to main content
15,915,702 members
Home / Discussions / C#
   

C#

 
GeneralRe: webservice problem Pin
WDI21-Jan-05 17:46
WDI21-Jan-05 17:46 
GeneralRe: webservice problem Pin
PatrickShane22-Jan-05 8:36
PatrickShane22-Jan-05 8:36 
GeneralRe: webservice problem Pin
WDI22-Jan-05 8:51
WDI22-Jan-05 8:51 
GeneralSending messages through an exchange server Pin
Esmo200021-Jan-05 9:08
Esmo200021-Jan-05 9:08 
GeneralRe: Sending messages through an exchange server Pin
Esmo200021-Jan-05 10:40
Esmo200021-Jan-05 10:40 
GeneralI need to create a movie file from my DirectDraw surfaces that I can play in PowerPoint (Please help me out) Pin
SIDDHARTH_JAIN21-Jan-05 9:05
SIDDHARTH_JAIN21-Jan-05 9:05 
GeneralDirectX.UnsafeNativeMethods Pin
thepersonof21-Jan-05 7:52
thepersonof21-Jan-05 7:52 
GeneralRe: DirectX.UnsafeNativeMethods Pin
leppie21-Jan-05 8:58
leppie21-Jan-05 8:58 
GeneralImageprocessing - Is there any color dropout algorithm for bitmap C# Pin
abcxyz8221-Jan-05 6:55
abcxyz8221-Jan-05 6:55 
Generalcalling events from a derived class Pin
Marc Clifton21-Jan-05 6:49
mvaMarc Clifton21-Jan-05 6:49 
GeneralRe: calling events from a derived class Pin
Nick Parker21-Jan-05 7:15
protectorNick Parker21-Jan-05 7:15 
GeneralRe: calling events from a derived class Pin
Corinna John21-Jan-05 7:19
Corinna John21-Jan-05 7:19 
GeneralRe: calling events from a derived class Pin
Heath Stewart21-Jan-05 7:24
protectorHeath Stewart21-Jan-05 7:24 
GeneralRe: calling events from a derived class Pin
Heath Stewart21-Jan-05 7:20
protectorHeath Stewart21-Jan-05 7:20 
Why can't you just call EventName(this, EventArgs.Empty), for example? Partly due to a language limitation, which is because of how C# (and others) compiles events by default.

Take the following example, compiled as a DLL:
using System;
 
public class Test
{
  public event EventHandler Foo;
 
  protected virtual void OnFoo(EventArgs e)
  {
    if (Foo != null)
      Foo(this, e);
  }
}
Now let's look at the IL generated for this simple class following the event guidelines:
.class public auto ansi beforefieldinit Test
       extends [mscorlib]System.Object
{
  .field private class [mscorlib]System.EventHandler Foo
  .method public hidebysig specialname instance void 
          add_Foo(class [mscorlib]System.EventHandler 'value') cil managed synchronized
  {
    // Code size       24 (0x18)
    .maxstack  3
    IL_0000:  ldarg.0
    IL_0001:  ldarg.0
    IL_0002:  ldfld      class [mscorlib]System.EventHandler Test::Foo
    IL_0007:  ldarg.1
    IL_0008:  call       class [mscorlib]System.Delegate [mscorlib]System.Delegate::Combine(class [mscorlib]System.Delegate,
                                                                                            class [mscorlib]System.Delegate)
    IL_000d:  castclass  [mscorlib]System.EventHandler
    IL_0012:  stfld      class [mscorlib]System.EventHandler Test::Foo
    IL_0017:  ret
  } // end of method Test::add_Foo
 
  .method public hidebysig specialname instance void 
          remove_Foo(class [mscorlib]System.EventHandler 'value') cil managed synchronized
  {
    // Code size       24 (0x18)
    .maxstack  3
    IL_0000:  ldarg.0
    IL_0001:  ldarg.0
    IL_0002:  ldfld      class [mscorlib]System.EventHandler Test::Foo
    IL_0007:  ldarg.1
    IL_0008:  call       class [mscorlib]System.Delegate [mscorlib]System.Delegate::Remove(class [mscorlib]System.Delegate,
                                                                                           class [mscorlib]System.Delegate)
    IL_000d:  castclass  [mscorlib]System.EventHandler
    IL_0012:  stfld      class [mscorlib]System.EventHandler Test::Foo
    IL_0017:  ret
  } // end of method Test::remove_Foo
 
  .method family hidebysig newslot virtual 
          instance void  OnFoo(class [mscorlib]System.EventArgs e) cil managed
  {
    // Code size       22 (0x16)
    .maxstack  3
    IL_0000:  ldarg.0
    IL_0001:  ldfld      class [mscorlib]System.EventHandler Test::Foo
    IL_0006:  brfalse.s  IL_0015
 
    IL_0008:  ldarg.0
    IL_0009:  ldfld      class [mscorlib]System.EventHandler Test::Foo
    IL_000e:  ldarg.0
    IL_000f:  ldarg.1
    IL_0010:  callvirt   instance void [mscorlib]System.EventHandler::Invoke(object,
                                                                             class [mscorlib]System.EventArgs)
    IL_0015:  ret
  } // end of method Test::OnFoo
 
  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed
  {
    // Code size       7 (0x7)
    .maxstack  1
    IL_0000:  ldarg.0
    IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0006:  ret
  } // end of method Test::.ctor
 
  .event [mscorlib]System.EventHandler Foo
  {
    .addon instance void Test::add_Foo(class [mscorlib]System.EventHandler)
    .removeon instance void Test::remove_Foo(class [mscorlib]System.EventHandler)
  } // end of event Test::Foo
} // end of class Test
Notice that a private field Foo is defined as EventHandler. From a derived class, as you know, you can't access the private therefore you can't call Invoke on it, which invokes each of the combined delegates.

Some classes - like Windows Forms controls - store the EventHandler derivatives into the protected Control.Events, which you can get the combined delegate and invoke it yourself.

This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles] [My Blog]
GeneralRe: calling events from a derived class Pin
Marc Clifton21-Jan-05 7:27
mvaMarc Clifton21-Jan-05 7:27 
GeneralRe: calling events from a derived class Pin
Heath Stewart21-Jan-05 7:44
protectorHeath Stewart21-Jan-05 7:44 
GeneralAlternative view Pin
leppie21-Jan-05 9:10
leppie21-Jan-05 9:10 
GeneralOleDbDataReader Pin
Newbie_Toy21-Jan-05 6:19
Newbie_Toy21-Jan-05 6:19 
GeneralRe: OleDbDataReader Pin
Colin Angus Mackay21-Jan-05 6:54
Colin Angus Mackay21-Jan-05 6:54 
GeneralRe: OleDbDataReader Pin
Newbie_Toy21-Jan-05 6:58
Newbie_Toy21-Jan-05 6:58 
GeneralRe: OleDbDataReader Pin
Heath Stewart21-Jan-05 7:30
protectorHeath Stewart21-Jan-05 7:30 
GeneralUsing P/Invoke SendInput Pin
realmontanakid21-Jan-05 4:58
realmontanakid21-Jan-05 4:58 
GeneralRe: Using P/Invoke SendInput Pin
Nick Parker21-Jan-05 6:30
protectorNick Parker21-Jan-05 6:30 
GeneralRe: Using P/Invoke SendInput Pin
Dave Kreskowiak21-Jan-05 7:03
mveDave Kreskowiak21-Jan-05 7:03 
GeneralRead from .dbx-file Pin
larbo20421-Jan-05 4:36
larbo20421-Jan-05 4:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.