using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ServerCore; public static class LambdaSafeHelper { //========================================================================================= // with for //========================================================================================= public static void forLambda(IEnumerable source, Action action) { if (source is IList list) { for (int i = 0; i < list.Count; ++i) { var node_item = list[i]; action(node_item); } } else { throw new ArgumentException("Source must implement IList for indexed access !!!", nameof(source)); } } public static async Task forLambdaAsync(IEnumerable source, Func action) { if (source is IList list) { for (int i = 0; i < list.Count; ++i) { var node_item = list[i]; await action(node_item); } } else { throw new ArgumentException("Source must implement IList for indexed access !!!", nameof(source)); } } public static void forLambdaSafe(IEnumerable source, Action action, Action onException) { if (source is IList list) { for (int i = 0; i < list.Count; ++i) { var node_item = list[i]; try { action(node_item); } catch (Exception e) { onException(e); } } } else { throw new ArgumentException("Source must implement IList for indexed access !!!", nameof(source)); } } public static async Task forLambdaSafeAsync( IEnumerable source , Func action, Func onException ) { if (source is IList list) { for (int i = 0; i < list.Count; ++i) { var node_item = list[i]; try { await action(node_item); } catch (Exception e) { await onException(e); } } } else { throw new ArgumentException("Source must implement IList for indexed access !!!", nameof(source)); } } //========================================================================================= // with foreach //========================================================================================= public static void forEachLambda(IEnumerable source, Action action) { foreach (var node_item in source) { action(node_item); } } public static async Task forEachLambdaAsync(IEnumerable source, Func action) { foreach (var node_item in source) { await action(node_item); } } public static void forEachLambdaSafe(IEnumerable source, Action action, Action onException) { foreach (var node_item in source) { try { action(node_item); } catch (Exception e) { onException(e); } } } public static async Task forEachLambdaSafeAsync(IEnumerable source, Func action, Func onException) { foreach (var node_item in source) { try { await action(node_item); } catch (Exception e) { await onException(e); } } } //========================================================================================= // with while //========================================================================================= public static void whileWithConditionLambda(IEnumerable source, Func condition, Action action) { using var enumerator = source.GetEnumerator(); while (enumerator.MoveNext()) { var node_item = enumerator.Current; if (false == condition(node_item)) { break; } action(node_item); } } public static async Task whileWithConditionLambdaAsync(IEnumerable source, Func> condition, Func action) { using var enumerator = source.GetEnumerator(); while (enumerator.MoveNext()) { var node_item = enumerator.Current; if (false == await condition(node_item)) { break; } await action(node_item); } } public static void whileWithConditionLambdaSafe(IEnumerable source, Func condition, Action action, Action onException) { using var enumerator = source.GetEnumerator(); while (enumerator.MoveNext()) { var node_item = enumerator.Current; if (false == condition(node_item)) { break; } try { action(node_item); } catch (Exception e) { onException(e); } } } public static async Task whileWithConditionLambdaSafeAsync(IEnumerable source, Func> condition, Func action, Func onException) { using var enumerator = source.GetEnumerator(); try { while (enumerator.MoveNext()) { var node_item = enumerator.Current; if (false == await condition(node_item)) { break; } await action(node_item); } } catch (Exception e) { await onException(e); } } //========================================================================================= // try-catch only //========================================================================================= public static void lambdaSafe(Action action, Action? onException = null) { try { action(); } catch (Exception e) { onException?.Invoke(e); } } public static async Task lambdaSafeAsync(Func action, Func? onException = null) { try { await action(); } catch (Exception e) { if (onException != null) { await onException(e); } } } }