when an event has multiple subscribers, how do I get the return value for each subscriber?

时光怂恿深爱的人放手 提交于 2019-11-28 06:57:01
Jason Kresowaty

Use Delegate.GetInvocationList.

if (SecondChange != null)
{
    DateTime now = DateTime.Now;
    foreach (Delegate d in SecondChange.GetInvocationList())
    {
        Console.WriteLine(d.DynamicInvoke(now));
    }
}

is it good practice to just use Action/Func instead of manually declaring a delegate?

Yes. But I will point out that the best practice is for events to use EventHandler<T> instead of Func<..., TResult>. EventHandler<T> does not support return values, but you are somewhat justified in that there are a few .NET events that have return values. I would consider it better to have a settable property in a custom EventArgs subclass that you use as your T. This is the pattern we see in things like KeyEventArgs.Handled. In this way, you can use EventHandler<T> and the subscribers can also coordinate their responses to a limited extent by getting and setting this property.

I think it is perfectly fine to use Action/Func instead of the delegate.

BUT Events are not supposed to be used like that. They are triggered at indefinite time point, so you just don't know all the parameters.

What you really need is probably:

  1. Use polymorphism for clock.
  2. Use visitor/subscriber/observer patterns to get their values.

So the code will look like:

var theClock = new Clock();
theClock.AddSecondsSubscriber(new DisplayClock());
theClock.AddSecondsSubscriber(new LogClock());
theClock.RunAndExecuteVisitors( theBoolResultYouNeed => Console.WriteLine(theBoolResultYouNeed) );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!