Click here to Skip to main content
15,896,207 members
Home / Discussions / C#
   

C#

 
Questiontext editor Pin
talk2krish15-Oct-05 21:08
talk2krish15-Oct-05 21:08 
AnswerRe: text editor Pin
Robert Rohde15-Oct-05 22:07
Robert Rohde15-Oct-05 22:07 
Questiondirectx video source as a binary stream? Pin
serguey_haftrige15-Oct-05 17:08
serguey_haftrige15-Oct-05 17:08 
AnswerRe: directx video source as a binary stream? Pin
Heath Stewart16-Oct-05 0:33
protectorHeath Stewart16-Oct-05 0:33 
GeneralRe: directx video source as a binary stream? Pin
serguey_haftrige16-Oct-05 14:52
serguey_haftrige16-Oct-05 14:52 
AnswerRe: directx video source as a binary stream? Pin
Heath Stewart16-Oct-05 17:28
protectorHeath Stewart16-Oct-05 17:28 
QuestionRemoving Handler... URGENT Pin
Shubhabrata Mohanty15-Oct-05 16:14
Shubhabrata Mohanty15-Oct-05 16:14 
AnswerRe: Removing Handler... URGENT Pin
Heath Stewart16-Oct-05 0:04
protectorHeath Stewart16-Oct-05 0:04 
The EventInfo contains information about an event, which describes add and remove accessors. It also has a method to remove an event handler, EventInfo.RemoveEventHandler. In order to call this you must get the delegate for the event handler (the target method).

To do this you need to get the field corresponding to the event that is the delegate. By default in C# at least, that field has the same name as the event. So, you can use EventInfo.Name then call Type.GetField with the name and the right BindingFlags. Cast the field value to Delegate and then you can get the invocation list, i.e. each target added to the delegate (a multicast delegate).

Below is a quick sample I threw together.
using System;
using System.Reflection;
 
class Test
{
  public event EventHandler TestEvent;
 
  public static void Main(string[] args)
  {
    Test t = new Test();
 
    Console.WriteLine("Adding event handlers");
    for (int i = 0; i < 5; i++)
    {
      t.TestEvent += new EventHandler(new Test(i).OnTestEvent);
      Console.WriteLine("Added event handler to test " + i);
    }
 
    Console.WriteLine("Calling event");
    if (t.TestEvent != null)
    {
      t.TestEvent(t, EventArgs.Empty);
    }
 
    Console.WriteLine("Removing event handlers");
    RemoveAllHandlers(t);
 
    Console.WriteLine("Calling event again");
    if (t.TestEvent != null)
    {
      t.TestEvent(t, EventArgs.Empty);
    }
  }
 
  Test()
  {
  }
 
  int index = 0;
  Test(int index)
  {
    this.index = index;
  }
 
  void OnTestEvent(object sender, EventArgs e)
  {
    Console.WriteLine("Handled from test " + index);
  }
 
  static void RemoveAllHandlers(Test test)
  {
    Type t = test.GetType();
    foreach (EventInfo ei in t.GetEvents())
    {
      // Assumes compiler-generated events.
      FieldInfo fi = t.GetField(ei.Name, BindingFlags.Public |
        BindingFlags.NonPublic | BindingFlags.Instance);
      if (fi != null)
      {
        Delegate d = (Delegate)fi.GetValue(test);
        foreach (Delegate handler in d.GetInvocationList())
        {
          Test target = handler.Target as Test;
          if (target != null)
          {
            Console.WriteLine("Removing event handler from test " + target.index);
          }
          ei.RemoveEventHandler(test, handler);
        }
      }
    }
  }
}


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

Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft

[My Articles] [My Blog]
GeneralRe: Removing Handler... URGENT Pin
Shubhabrata Mohanty16-Oct-05 11:06
Shubhabrata Mohanty16-Oct-05 11:06 
AnswerRe: Removing Handler... URGENT Pin
Heath Stewart16-Oct-05 17:06
protectorHeath Stewart16-Oct-05 17:06 
GeneralRe: Removing Handler... URGENT Pin
Shubhabrata Mohanty16-Oct-05 20:42
Shubhabrata Mohanty16-Oct-05 20:42 
AnswerRe: Removing Handler... URGENT Pin
Heath Stewart17-Oct-05 5:58
protectorHeath Stewart17-Oct-05 5:58 
QuestionEncode two integers Pin
JoaoPe15-Oct-05 15:48
JoaoPe15-Oct-05 15:48 
AnswerRe: Encode two integers Pin
Rob Philpott15-Oct-05 23:29
Rob Philpott15-Oct-05 23:29 
AnswerRe: Encode two integers Pin
Heath Stewart16-Oct-05 0:17
protectorHeath Stewart16-Oct-05 0:17 
GeneralRe: Encode two integers Pin
JoaoPe16-Oct-05 2:33
JoaoPe16-Oct-05 2:33 
AnswerRe: Encode two integers Pin
Heath Stewart16-Oct-05 17:14
protectorHeath Stewart16-Oct-05 17:14 
QuestionWinService issues Pin
WetRivrRat15-Oct-05 13:42
WetRivrRat15-Oct-05 13:42 
AnswerRe: WinService issues Pin
Heath Stewart16-Oct-05 0:25
protectorHeath Stewart16-Oct-05 0:25 
QuestionRead until &quot;- -&quot; ??? Pin
WetRivrRat15-Oct-05 13:06
WetRivrRat15-Oct-05 13:06 
AnswerRe: Read until &quot;- -&quot; ??? Pin
Robert Rohde15-Oct-05 22:29
Robert Rohde15-Oct-05 22:29 
AnswerRe: Read until &quot;- -&quot; ??? Pin
Scott Serl16-Oct-05 15:00
Scott Serl16-Oct-05 15:00 
Questionvideo streaming, Is it possible Pin
serguey_haftrige15-Oct-05 12:23
serguey_haftrige15-Oct-05 12:23 
AnswerRe: video streaming, Is it possible Pin
leppie15-Oct-05 23:24
leppie15-Oct-05 23:24 
QuestionPersistence Pin
Sled Dog15-Oct-05 11:27
Sled Dog15-Oct-05 11:27 

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.