锅炉板code替换 - 有什么不好这个code? [英] Boiler plate code replacement - is there anything bad about this code?
问题描述
我最近创建了这两个(无关)的方法来取代大量的样板code在我的WinForms应用程序。据我所知,他们的工作不错,但我需要就是否有一些问题我可能会丢失。
一些安慰/咨询(从内存中)
静态类SafeInvoker
{
//工具,以避免锅炉板InvokeRequired code
//用法:SafeInvoker.Invoke(myCtrl,()=> myCtrl.Enabled = FALSE);
公共静态无效调用(控制CTRL,动作CMD)
{
如果(ctrl.InvokeRequired)
ctrl.BeginInvoke(新MethodInvoker(CMD));
其他
CMD();
} //替换OnMyEventRaised锅炉板code
//用法:SafeInvoker.RaiseEvent(这一点,MyEventRaised)
公共静态无效的RaiseEvent(对象发件人,事件处理程序EVNT)
{
VAR处理器= EVNT;
如果(处理!= NULL)
处理器(发件人,EventArgs.Empty);
}
}
编辑:请参阅相关的问题<一href=\"http://stackoverflow.com/questions/258409/how-to-get-information-about-an-exception-raised-by-the-target-of-controlinvoke\">here
更新
这是问题的僵局继(在<一个有关href=\"http://stackoverflow.com/questions/2055960/control-invoke-getting-stuck-in-hidden-showdialog\">this问题),我从调用切换到的BeginInvoke(查看说明<一个href=\"http://stackoverflow.com/questions/229554/whats-the-difference-between-invoke-and-begininvoke/229558#229558\">here).
另一个更新
对于第二个片段,我越来越倾向于使用'空委托模式,它通过直接与空处理程序声明事件,像这样'源头'修复此问题:
事件的EventHandler MyEventRaised =委托{};
这是好东西。让他们扩展方法虽然清理你的code多一点。例如:
//替换OnMyEventRaised锅炉板code
//用法:SafeInvoker.RaiseEvent(这一点,MyEventRaised)
公共静态无效抬起(此事件处理eventToRaise,对象发件人)
{
事件处理事件处理程序= eventToRaise; 如果(事件处理!= NULL)
事件处理程序(发件人,EventArgs.Empty);
}
现在在你的事件,你可以拨打:myEvent.Raise(本);
I've recently created these two (unrelated) methods to replace lots of boiler-plate code in my winforms application. As far as I can tell, they work ok, but I need some reassurance/advice on whether there are some problems I might be missing.
(from memory)
static class SafeInvoker
{
//Utility to avoid boiler-plate InvokeRequired code
//Usage: SafeInvoker.Invoke(myCtrl, () => myCtrl.Enabled = false);
public static void Invoke(Control ctrl, Action cmd)
{
if (ctrl.InvokeRequired)
ctrl.BeginInvoke(new MethodInvoker(cmd));
else
cmd();
}
//Replaces OnMyEventRaised boiler-plate code
//Usage: SafeInvoker.RaiseEvent(this, MyEventRaised)
public static void RaiseEvent(object sender, EventHandler evnt)
{
var handler = evnt;
if (handler != null)
handler(sender, EventArgs.Empty);
}
}
EDIT: See related question here
UPDATE
Following on from deadlock problems (related in this question), I have switched from Invoke to BeginInvoke (see an explanation here).
Another Update
Regarding the second snippet, I am increasingly inclined to use the 'empty delegate' pattern, which fixes this problem 'at source' by declaring the event directly with an empty handler, like so:
event EventHandler MyEventRaised = delegate {};
This is good stuff. Make them extension methods though to clean up your code a little more. For example:
//Replaces OnMyEventRaised boiler-plate code
//Usage: SafeInvoker.RaiseEvent(this, MyEventRaised)
public static void Raise(this EventHandler eventToRaise, object sender)
{
EventHandler eventHandler = eventToRaise;
if (eventHandler != null)
eventHandler(sender, EventArgs.Empty);
}
Now on your events you can call: myEvent.Raise(this);
这篇关于锅炉板code替换 - 有什么不好这个code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!