Clearing event handlers
-
Is there some way anyone knows to clear out the handlers for an event? An object I've created needs to clear all handlers to it at a point in the code where it is unknown what is attached to it. You can't just set the event to
null
, the compiler complains about that. I've also tried the following to no effect:... if (MyEvent != null) { Delegate[] delegateList = MyEvent.GetInvocationList(); foreach (MyEventHandler meh in delegateList) { MyEvent -= meh; } } ...
I thought it showed promise (the compiler didn't complain, and it did run fine) - unfortunately it simply didn't do anything. I've also tried some variations to the above, using the normal way of decoupling from an event, but by trying some of the values of the delegate to get the method:... if (MyEvent != null) { Delegate[] delegateList = MyEvent.GetInvocationList(); foreach (MyEventHandler meh in delegateList) { MyEvent -= new MyEventHandler(meh.Method); } } ...
- but the Delegate.Method property (as well as it's properties) don't seem to contain the right value wanted (the compiler makes noise about the fact that a property is being passed in to the handler instead of a method). I think I'm on the right track, but seem to be missing some key element to make it work. Anyone have any ideas? ----- In the land of the blind, the one eyed man is king. -
Is there some way anyone knows to clear out the handlers for an event? An object I've created needs to clear all handlers to it at a point in the code where it is unknown what is attached to it. You can't just set the event to
null
, the compiler complains about that. I've also tried the following to no effect:... if (MyEvent != null) { Delegate[] delegateList = MyEvent.GetInvocationList(); foreach (MyEventHandler meh in delegateList) { MyEvent -= meh; } } ...
I thought it showed promise (the compiler didn't complain, and it did run fine) - unfortunately it simply didn't do anything. I've also tried some variations to the above, using the normal way of decoupling from an event, but by trying some of the values of the delegate to get the method:... if (MyEvent != null) { Delegate[] delegateList = MyEvent.GetInvocationList(); foreach (MyEventHandler meh in delegateList) { MyEvent -= new MyEventHandler(meh.Method); } } ...
- but the Delegate.Method property (as well as it's properties) don't seem to contain the right value wanted (the compiler makes noise about the fact that a property is being passed in to the handler instead of a method). I think I'm on the right track, but seem to be missing some key element to make it work. Anyone have any ideas? ----- In the land of the blind, the one eyed man is king.Vineas wrote:
...
if (MyEvent != null)
{
Delegate[] delegateList = MyEvent.GetInvocationList();
foreach (MyEventHandler meh in delegateList)
{
MyEvent -= new MyEventHandler(meh.Method);
}
}Try using:
if (MyEvent != null)
{
Delegate[] delegateList = MyEvent.GetInvocationList();
foreach (MyEventHandler meh in delegateList)
{
MyEvent -= meh;
}
]Basically your trying to remove a delegate that you've only just created, me thinks, what you need is a reference to the actual delegate as the above code demonstrates. Ed
-
Vineas wrote:
...
if (MyEvent != null)
{
Delegate[] delegateList = MyEvent.GetInvocationList();
foreach (MyEventHandler meh in delegateList)
{
MyEvent -= new MyEventHandler(meh.Method);
}
}Try using:
if (MyEvent != null)
{
Delegate[] delegateList = MyEvent.GetInvocationList();
foreach (MyEventHandler meh in delegateList)
{
MyEvent -= meh;
}
]Basically your trying to remove a delegate that you've only just created, me thinks, what you need is a reference to the actual delegate as the above code demonstrates. Ed
Ed.Poore wrote:
Try using:
if (MyEvent != null) { Delegate[] delegateList = MyEvent.GetInvocationList(); foreach (MyEventHandler meh in delegateList) { MyEvent -= meh; } }
Basically your trying to remove a delegate that you've only just created, me thinks, what you need is a reference to the actual delegate as the above code demonstrates. Eduh - did you read my initial post?!?!?!? That is exactly the first code snippet I listed (which does not work BTW - which then prompted me to continue to the other snippet, which also didn't work). I still haven't found a solution to this. Some of the things I've found when searching around seem to indicate that others have tried and failed - so in the interest of brevity (and better code), for my own purposes, I re-did the section in question so am no longer facing this issue.* I am still interested in a solution though - there have been other times I've wanted to do this, but haven't had the time to go much further - and from my searches I know others have wanted a solution as well. * in case anyone was wondering why this was needed to begin with - some code I wrote a while ago was doing a deep copy on some different objects by doing a MemberwiseClone(), then selectively doing deep copies on reference types in that object. This worked OK until I recently found that during the MemberwiseClone() call, an event from a base object was bringing all it's handlers with it when copied. D'oh! I wanted a way to clear out the event after copying - but it was better to rewrite the copy code to work correctly than to continue with a bad idea implemented because of a time crunch. ----- In the land of the blind, the one eyed man is king.
-
Ed.Poore wrote:
Try using:
if (MyEvent != null) { Delegate[] delegateList = MyEvent.GetInvocationList(); foreach (MyEventHandler meh in delegateList) { MyEvent -= meh; } }
Basically your trying to remove a delegate that you've only just created, me thinks, what you need is a reference to the actual delegate as the above code demonstrates. Eduh - did you read my initial post?!?!?!? That is exactly the first code snippet I listed (which does not work BTW - which then prompted me to continue to the other snippet, which also didn't work). I still haven't found a solution to this. Some of the things I've found when searching around seem to indicate that others have tried and failed - so in the interest of brevity (and better code), for my own purposes, I re-did the section in question so am no longer facing this issue.* I am still interested in a solution though - there have been other times I've wanted to do this, but haven't had the time to go much further - and from my searches I know others have wanted a solution as well. * in case anyone was wondering why this was needed to begin with - some code I wrote a while ago was doing a deep copy on some different objects by doing a MemberwiseClone(), then selectively doing deep copies on reference types in that object. This worked OK until I recently found that during the MemberwiseClone() call, an event from a base object was bringing all it's handlers with it when copied. D'oh! I wanted a way to clear out the event after copying - but it was better to rewrite the copy code to work correctly than to continue with a bad idea implemented because of a time crunch. ----- In the land of the blind, the one eyed man is king.