Automatically unsubscribe an event

Posted by Cat on Stack Overflow See other posts from Stack Overflow or by Cat
Published on 2010-05-03T16:48:25Z Indexed on 2010/05/03 17:18 UTC
Read the original article Hit count: 201

Filed under:
|

Is it possible to do something like this in C#? Probably with Expressions.

private void RegisterEvent(EventHandler handler, Action<EventArgs> action)
{
   handler += action;
   m_toUnsubscribe.Add(handler, action);
}

... in Dispose()
{
  foreach(var pair in m_toUnsubscribe)
  {
    pair.handler -= pair.action;
  }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about events