1

So I have an EventHandler:

public event EventHandler<CustomEventArgs> Confirmed;

protected void RaiseConfirmed()
{
    Confirmed?.Invoke(this, new CustomEventArgs(Color.red));
}

And I have a couple of event listeners in different classes, such as.


public void SetColor(object sender, CustomEventArgs e)
{
    //do something with e.Color
}

But that's not exactly what I want.

Is it possible to raise the event, but send different EventArgs to the different listeners?

So when the Confirm button is clicked, and the Confirmed event is fired, I want to set one listener to Color red, the other to Color blue, and yet another listener to Color green. And perhaps I have yet another listener that does not take a color, but a bool value.

So essentially, the sender does not really know about the argument. The argument is injected before the receiver receives it.

Does that make sense? Should I implement my own event handling to support different arguments per listener?

WavyRancheros
  • 121
  • 1
  • 4
  • 2
    You *can* do this using `Confirmed.GetInvocationList()` and manually invoking each subscriber, but that smells -- this is unexpected, and it's not how events normally work. I'd lean towards rolling your own system here. – canton7 Mar 23 '22 at 10:42
  • How would the Listener determine if it is the "first", "second" or nth listener? Theoretically, you can instead of using `Confirmed?.Invoke` loop the `GetInvocationList()` ... but I doubt you will be able to guarantee an order. As canton7 writes: You may opt for a custom messaging system. – Fildor Mar 23 '22 at 10:43

0 Answers0