How to use event in method argument? I'm trying to write method to unsubscribe all anonymous event handlers. In this example UnsubscribeAll()
is working, but UnsubscribeAll(Action eventAction)
isn't.
public class Foo
{
public event Action SomeEvent;
public void UnsubscribeAll(Action eventAction)
{
if(eventAction is null)
return;
var delegates = eventAction.GetInvocationList();
for (int i = 0; i < delegates.Length; i++)
{
Action action = (Action) delegates[i];
if(action != null && SomeEvent != null)
eventAction -= action;
}
}
public void UnsubscribeAll()
{
if(SomeEvent is null)
return;
var delegates = SomeEvent.GetInvocationList();
for (int i = 0; i < delegates.Length; i++)
{
Action action = (Action) delegates[i];
if(action != null && SomeEvent != null)
SomeEvent -= action;
}
}
}