Created
March 1, 2017 20:55
-
-
Save jonHuffman/e54210095c865141620d6ca29509bc23 to your computer and use it in GitHub Desktop.
Adds a SafeInvoke method for C# Actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public static class ActionExtensions | |
{ | |
public static void SafeInvoke(this Action action) | |
{ | |
if(action != null) | |
{ | |
action(); | |
} | |
} | |
public static void SafeInvoke<T>(this Action<T> action, T arg) | |
{ | |
if (action != null) | |
{ | |
action(arg); | |
} | |
} | |
public static void SafeInvoke<T1, T2>(this Action<T1, T2> action, T1 arg1, T2 arg2) | |
{ | |
if (action != null) | |
{ | |
action(arg1, arg2); | |
} | |
} | |
public static void SafeInvoke<T1, T2, T3>(this Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3) | |
{ | |
if (action != null) | |
{ | |
action(arg1, arg2, arg3); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment