namespace System
{
    public static class ActionUtils
    {
        /// <summary>
        /// Action的安全回调
        /// </summary>
        /// <param name="action"></param>
        public static void SafeInvoke(this Action action)
        {
            try
            {
                action?.Invoke();
            }
            catch (Exception ex)
            {
                XGame.Log.Exception(ex);
            }
        }
        /// <summary>
        /// Action的安全回调
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="action"></param>
        /// <param name="obj"></param>
        public static void SafeInvoke<T>(this Action<T> action, T obj)
        {
            try
            {
                action?.Invoke(obj);
            }
            catch (Exception ex)
            {
                XGame.Log.Exception(ex);
            }
        }
        /// <summary>
        /// Action的安全回调
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="U"></typeparam>
        /// <param name="action"></param>
        /// <param name="argT"></param>
        /// <param name="argU"></param>
        public static void SafeInvoke<T, U>(this Action<T, U> action, T argT, U argU)
        {
            try
            {
                action?.Invoke(argT, argU);
            }
            catch (Exception ex)
            {
                XGame.Log.Exception(ex);
            }
        }
        /// <summary>
        /// Action的安全回调
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="U"></typeparam>
        /// <typeparam name="V"></typeparam>
        /// <param name="action"></param>
        /// <param name="argT"></param>
        /// <param name="argV"></param>
        public static void SafeInvoke<T, U, V>(this Action<T, U, V> action, T argT, U argU, V argV)
        {
            try
            {
                action?.Invoke(argT, argU, argV);
            }
            catch (Exception ex)
            {
                XGame.Log.Exception(ex);
            }
        }
        /// <summary>
        /// Func的安全回调
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="func"></param>
        /// <returns></returns>
        public static TResult SafeInvoke<TResult>(this Func<TResult> func)
        {
            TResult result = default;
            try
            {
                if (func != null)
                    result = func.Invoke();
            }
            catch (Exception ex)
            {
                XGame.Log.Exception(ex);
            }
            return result;
        }
        /// <summary>
        /// Func的安全回调
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="func"></param>
        /// <param name="argT"></param>
        /// <returns></returns>
        public static TResult SafeInvoke<T, TResult>(this Func<T, TResult> func, T argT)
        {
            TResult result = default;
            try
            {
                if (func != null)
                    result = func.Invoke(argT);
            }
            catch (Exception ex)
            {
                XGame.Log.Exception(ex);
            }
            return result;
        }
    }
}
