Detach an event based on an anonymous method?

B

Barry Mossman

Hi, is it possible to detach an event which is implemented via an anonymous
method.

ie. If the code is:
myObject.myEvent += delegate(object sender, EventArgs e) {
// snip event actions
}

is there a way that I can I detach this later via -=, or do I need to use a
named event?

thanks
Barry Mossman
 
M

Mattias Sjögren

Barry,
Hi, is it possible to detach an event which is implemented via an anonymous
method.

Only if you keep a reference to the delegate.

EventHandler x = delegate(object sender, EventArgs e) {
// snip event actions
}
myObject.myEvent += x;
....
myObject.myEvent -= x;


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top