Click here to Skip to main content
15,905,414 members
Home / Discussions / C#
   

C#

 
GeneralRe: A shell extension for a virtual drive Pin
Dave Kreskowiak16-Oct-05 9:14
mveDave Kreskowiak16-Oct-05 9:14 
GeneralRe: A shell extension for a virtual drive Pin
iulian.bobalca16-Oct-05 12:07
iulian.bobalca16-Oct-05 12:07 
GeneralRe: A shell extension for a virtual drive Pin
Dave Kreskowiak16-Oct-05 15:07
mveDave Kreskowiak16-Oct-05 15:07 
AnswerRe: A shell extension for a virtual drive Pin
Heath Stewart16-Oct-05 17:35
protectorHeath Stewart16-Oct-05 17:35 
GeneralRe: A shell extension for a virtual drive Pin
leppie16-Oct-05 20:18
leppie16-Oct-05 20:18 
GeneralRe: A shell extension for a virtual drive Pin
Dave Kreskowiak17-Oct-05 1:21
mveDave Kreskowiak17-Oct-05 1:21 
AnswerRe: A shell extension for a virtual drive Pin
Heath Stewart16-Oct-05 17:38
protectorHeath Stewart16-Oct-05 17:38 
QuestionEncapsulate Pin
Tran Ngoc Minh15-Oct-05 23:59
Tran Ngoc Minh15-Oct-05 23:59 
AnswerRe: Encapsulate Pin
Guffa16-Oct-05 0:02
Guffa16-Oct-05 0:02 
AnswerRe: Encapsulate Pin
Heath Stewart16-Oct-05 0:29
protectorHeath Stewart16-Oct-05 0:29 
QuestionCreating Libraries in C# Pin
ddmcr15-Oct-05 22:35
ddmcr15-Oct-05 22:35 
AnswerRe: Creating Libraries in C# Pin
S. Senthil Kumar15-Oct-05 23:10
S. Senthil Kumar15-Oct-05 23:10 
GeneralRe: Creating Libraries in C#[modified] Pin
ddmcr15-Oct-05 23:21
ddmcr15-Oct-05 23:21 
GeneralRe: Creating Libraries in C#[modified] Pin
S. Senthil Kumar16-Oct-05 1:03
S. Senthil Kumar16-Oct-05 1:03 
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 

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.