当一个事件有多个订户,我如何获得每个用户的返回值? [英] when an event has multiple subscribers, how do I get the return value for each subscriber?
问题描述
代码如下所示:
时钟:
公共类时钟
{
公共事件Func键<日期时间,布尔> SecondChange;
公共无效的run()
{
为(VAR I = 0; I< 20;我++)
{
Thread.sleep代码(1000 );
如果(SecondChange!= NULL)
{
//我如何获得每个用户的返回值?
Console.WriteLine(SecondChange(DateTime.Now));
}
}
}
}
DisplayClock
公共类DisplayClock
{
公共静态布尔TimeHasChanged(DateTime的现在)
{
Console.WriteLine(now.ToShortTimeString()+显示);
返回真;
}
}
LogClock:
公共类LogClock
{
公共静态布尔WriteLogEntry(现在的日期时间)
{
Console.WriteLine(现。 ToShortTimeString()+日志);
返回FALSE;
}
}
要运行该代码:
VAR theClock =新时钟();
theClock.SecondChange + = DisplayClock.TimeHasChanged;
theClock.SecondChange + = LogClock.WriteLogEntry;
theClock.Run();
其他的问题是:
- 是不是对每个用户返回一个值?
- 它是很好的做法,只是声明动作/ FUNC作为此次活动的回报类型,而不是手动声明的委托?
好的做法
使用委派。 GetInvocationList
。
如果(SecondChange!= NULL)
{
的DateTime现在= DateTime.Now;
的foreach(在SecondChange.GetInvocationList代表D())
{
Console.WriteLine(d.DynamicInvoke(现));
}
}
它是很好的做法只使用Action / FUNC,而不是手动声明的委托?
块引用>
是的。但我会指出,最好的做法是对事件进行使用
事件处理程序< T>
而不是Func键< ...,TResult>
。事件处理程序< T>
不支持返回值,但你是有些说不过去的,有是有返回值的几个.NET事件。我会认为这是最好有一个可设置属性在自定义的,你为你的T使用
EventArgs的
子类。这就好比KeyEventArgs.Handled
我们看到事物的格局。通过这种方式,你可以使用事件处理程序< T>
和用户也可以通过获取和设置该属性协调其在有限范围内的反应The code looks like below:
Clock:
public class Clock { public event Func<DateTime, bool> SecondChange; public void Run() { for (var i = 0; i < 20; i++) { Thread.Sleep(1000); if (SecondChange != null) { //how do I get return value for each subscriber? Console.WriteLine(SecondChange(DateTime.Now)); } } } }
DisplayClock:
public class DisplayClock { public static bool TimeHasChanged(DateTime now) { Console.WriteLine(now.ToShortTimeString() + " Display"); return true; } }
LogClock:
public class LogClock { public static bool WriteLogEntry(DateTime now) { Console.WriteLine(now.ToShortTimeString() + " Log"); return false; } }
To run the code:
var theClock = new Clock(); theClock.SecondChange += DisplayClock.TimeHasChanged; theClock.SecondChange += LogClock.WriteLogEntry; theClock.Run();
The other questions are:
- Is it good practice for each subscriber to return a value?
- Is it good practice to just declare Action/Func as the event return type instead of manually declaring a delegate?
解决方案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 ofFunc<..., 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 customEventArgs
subclass that you use as yourT
. This is the pattern we see in things likeKeyEventArgs.Handled
. In this way, you can useEventHandler<T>
and the subscribers can also coordinate their responses to a limited extent by getting and setting this property.这篇关于当一个事件有多个订户,我如何获得每个用户的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!