Boiler plate code replacement - is there anything bad about this code?
Due to the fact, that Benjol doesn't know, why he places the Action into a MethodInvoker and broccliman meant to use it as an Extension Function, here is the clean up code:
static class SafeInvoker
{
//Utility to avoid boiler-plate InvokeRequired code
//Usage: myCtrl.SafeInvoke(() => myCtrl.Enabled = false);
public static void SafeInvoke(this Control ctrl, Action cmd)
{
if (ctrl.InvokeRequired)
ctrl.BeginInvoke(cmd);
else
cmd();
}
//Replaces OnMyEventRaised boiler-plate code
//Usage: this.RaiseEvent(myEventRaised);
public static void RaiseEvent(this object sender, EventHandler evnt)
{
if (evnt != null)
evnt(sender, EventArgs.Empty);
}
}
Just a last note: MethodInvoker
and Action
are both just delegates having the exact same structure. Due to this case both are replaceable by each other. The root of this naming clash comes from legacy. At the beginning (.Net 2.0) there was just MethodInvoker
and Action(T)
. But due to the fact, that everyone who used Action(T)
whishes to have a Action
and found it very unnatural to take MethodInvoker
. So in .Net 3.5 the Action
, Action(T1, T2, T3, T4)
and all the Func
delegates where added too, but MethodInvoker could not be removed anymore without making any breaking changes.
Additional:
If you are able to use .Net 3.5 the above code is fine, but if you're pinned to .Net 2.0 you can use it as normal function as before and replace Action
by MethodInvoker
.
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);