I'm sure I'm not the first to write these extension methods, and if I am I would be amazed. Here is a small static class to make the invoking of events more readable. When you invoke an event, the code might look like the following.
if (OnCreation != null)
OnCreation.Invoke(sender,e);
I reduced it to an extenstion method that looks something like this.
// No sender or event arguments
OnCreation.IsNotNullThenInvoke();
// Only with sender
OnCreation.IsNotNullThenInvoke(this);
// With sender and event arguments
OnCreation.IsNotNullThenInvoke(this, e);
And here is the code.
public static class EventExtensionMethods
{
public static void IsNotNullThenInvoke(this EventHandler target)
{
target.IsNotNullThenInvoke(null,EventArgs.Empty);
}
public static void IsNotNullThenInvoke(this EventHandler target, object sender)
{
target.IsNotNullThenInvoke(sender,EventArgs.Empty);
}
public static void IsNotNullThenInvoke(this EventHandler target,
object sender, EventArgs e)
{
if (target != null)
target.Invoke(sender,e);
}
}
There you have it, a single method that extends events. Less code, makes your code easier to read. Hope this helps.
Note: There is no validation on the arguments being passed to the extension methods, this means you could pass in null arguments. In terms of events, I feel this is ok.