接受 (EventArgs) 的方法的包装器
我正在尝试实现 EventArgs 以将参数列表传递到我的消息传递系统:问题。
我对 EventArgs 进行了子类化:
public class SingleParameterArgs<T> : EventArgs
{
public T arg1;
public SingleParameterArgs(T _arg1)
{
arg1 = _arg1;
}
}
这是应该接受 EventArgs 的类和方法:
static public class Messenger<TEventArgs> where TEventArgs : EventArgs {
private static Dictionary< string, EventHandler<TEventArgs> > eventTable = new Dictionary< string, EventHandler<TEventArgs> >();
static public void Invoke(string eventType, TEventArgs args) {
EventHandler<TEventArgs> eventHandler;
if (eventTable.TryGetValue(eventType, out eventHandler)) {
if (eventHandler != null)
eventHandler();
}
}
}
在实现 EventArgs 之前,我将通过以下方式调用消息:
Messenger<GameEndingType>.Invoke( "end game", GameEndingType.TimeEnded );
但现在它看起来更长、更复杂:
Messenger< SingleParameterArgs<GameEndingType> >.Invoke( "end game", new SingleParameterArgs<GameEndingType>(GameEndingType.TimeEnded) );
是否可以使其看起来更短?我不想每次需要发送消息时都输入这么长的一行。也许我可以创建一个包装器? 像这样的事情就很完美了: Messenger.Invoke("游戏结束", GameEndingType.TimeEnded);
为随机数量的参数创建统一包装器的最佳方法是什么?
I'm trying to implement EventArgs to pass a list of parameters to my messaging system: Question.
I subclassed EventArgs:
public class SingleParameterArgs<T> : EventArgs
{
public T arg1;
public SingleParameterArgs(T _arg1)
{
arg1 = _arg1;
}
}
Here's the class and method that should accept the EventArgs:
static public class Messenger<TEventArgs> where TEventArgs : EventArgs {
private static Dictionary< string, EventHandler<TEventArgs> > eventTable = new Dictionary< string, EventHandler<TEventArgs> >();
static public void Invoke(string eventType, TEventArgs args) {
EventHandler<TEventArgs> eventHandler;
if (eventTable.TryGetValue(eventType, out eventHandler)) {
if (eventHandler != null)
eventHandler();
}
}
}
Before implementing EventArgs I would invoke a message in the following way:
Messenger<GameEndingType>.Invoke( "end game", GameEndingType.TimeEnded );
But now it looks much longer and much more complicated:
Messenger< SingleParameterArgs<GameEndingType> >.Invoke( "end game", new SingleParameterArgs<GameEndingType>(GameEndingType.TimeEnded) );
Is it possible to make it look shorter? I don't want to type such a long line every time I need to send a message. Maybe I could create a wrapper?
Something like this would be perfect:
Messenger.Invoke("end game", GameEndingType.TimeEnded);
What is the best way to create a uniform wrapper for a random amount of parameters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您对
Messenger
类与SingleParameterArgs
绑定感到满意吗?如果是这样,您可以使用:当然,您可以同时拥有两个,一个完全通用的 Messenger 类(根据您的问题),然后是一个委托给它的
SingleParameterMessenger 类:
顺便说一句,我不太确定这是否是一个好主意 - 特别是在静态注册方面,这往往会使测试变得更加困难,并且当然在并发性方面需要更多的关注。 (您的代码目前不是线程安全的。)
Are you happy for your
Messenger
class to be tied toSingleParameterArgs<T>
? If so, you could use:Of course you can have both, with a totally general
Messenger
class (as per your question), and then aSingleParameterMessenger
class which delegates to it:Just as an aside, I'm not really sure this is all a good idea anyway - particularly in terms of static registration, which tends to make testing harder, and certainly needs more care in terms of concurrency. (Your code is currently not threadsafe.)