Created
October 21, 2023 18:35
-
-
Save liortal53/c3ef0110da0310c1f16394456dceab67 to your computer and use it in GitHub Desktop.
Example demonstrating how to safely invoke all delegates in a System.Action while catching exceptions
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; | |
namespace Extensions.Utils | |
{ | |
public static class ActionExtensions | |
{ | |
/// <summary> | |
/// Safe invocation of all delegates stored in the passed <see cref="Action"/> delegate. | |
/// </summary> | |
public static void InvokeSafe(this Action action) | |
{ | |
if (action == null) | |
{ | |
return; | |
} | |
foreach (Action a in action.GetInvocationList()) | |
{ | |
try | |
{ | |
a.Invoke(); | |
} | |
catch (Exception e) | |
{ | |
// TODO: Debug.LogException ? | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment