Net Core教程

C# 通用Try, Run

本文主要是介绍C# 通用Try, Run,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

商用时请保留Cnblog链接!

转载时请保留Cnblog链接!

    /// <summary>
    /// WK 自创Try,Run,Task, 不要问我为什么,脑子一热就写出这么多东西
    /// </summary>
    public static class TryCatchHelper
    {
        public static void Run(Action runAction,Action thenAction)
        {
            if (runAction != null)
                runAction();
            if (thenAction != null)
                thenAction();
        }
        public static void Run<DataType>(Func<DataType> runAction, Action<DataType> thenAction)
        {
            DataType runResult = default(DataType);
            if (runAction != null)
                runResult = runAction();
            if (thenAction != null)
                thenAction(runResult);
        }
        public static async void Run(Func<Task> runAction, Action thenAction)
        {
            if (runAction != null)
               await runAction();
            if (thenAction != null)
                thenAction();
        }
        public static async void Run<DataType>(Func<Task<DataType>> runAction, Action<DataType> thenAction)
        {
            DataType runResult = default(DataType);
            if (runAction != null)
                runResult = await runAction();
            if (thenAction != null)
                thenAction(runResult);
        }
        public static async void RunTask(Action runAction,Action thenAction)
        {
            if (runAction != null)
            {
                await Task.Run(runAction);
            }
            if (thenAction != null)
                thenAction();
        }
        public static async void RunTask<DataType>(Func<DataType> runAction, Action<DataType> thenAction)
        {
            DataType valueFromRunAction = default(DataType);
            if (runAction != null)
            {
                valueFromRunAction = await Task.Run(runAction);
            }
            if (thenAction != null)
                thenAction(valueFromRunAction);
        }


        public static void TryCatch(Action TryFunction, Action SuccessFunction)
        {
            try
            {
                if (TryFunction != null)
                {
                    TryFunction();
                }
                if (SuccessFunction != null)
                {
                    SuccessFunction();
                }
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine(exp.Message);
            }
        }
        public static void TryCatch<DataType>(Func<DataType> TryFunction, Action<DataType> SuccessFunction)
        {
            DataType valueFromTry = default(DataType);
            try
            {
                if (TryFunction != null)
                {
                    valueFromTry = TryFunction();
                }
                if (SuccessFunction != null)
                {
                    SuccessFunction(valueFromTry);
                }
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine(exp.Message);
            }
        }
        public static void TryCatch(Action TryFunction, Action<Exception> CatchFunction, Action FinallyFunction)
        {
            try
            {
                if (TryFunction != null)
                    TryFunction();
            }
            catch (Exception exp)
            {
                if (CatchFunction != null)
                    CatchFunction(exp);
            }
            finally
            {
                if (FinallyFunction != null)
                {
                    FinallyFunction();
                }
            }
        }
        public static void TryCatch(Action TryFunction, Action SuccessFunction, Action<Exception> CatchFunction, Action FinallyFunction)
        {
            try
            {
                if (TryFunction != null)
                    TryFunction();
                if (SuccessFunction != null)
                    SuccessFunction();
            }
            catch (Exception exp)
            {
                if (CatchFunction != null)
                    CatchFunction(exp);
            }
            finally
            {
                if (FinallyFunction != null)
                {
                    FinallyFunction();
                }
            }
        }
        public static void TryCatch<DataType>(Func<DataType> TryFunction, Action<DataType> SuccessFunction, Action<Exception> CatchFunction, Action FinallyFunction)
        {
            DataType result = default(DataType);
            try
            {
                if (TryFunction != null)
                    result = TryFunction();
                if (SuccessFunction != null)
                    SuccessFunction(result);
            }
            catch (Exception exp)
            {
                if (CatchFunction != null)
                    CatchFunction(exp);
            }
            finally
            {
                if (FinallyFunction != null)
                {
                    FinallyFunction();
                }
            }
        }
        

        public static async void TryTaskRun(Action TryFunction, Action SuccessFunction)
        {
            try
            {
                if (TryFunction != null)
                {
                    await Task.Run(TryFunction);
                }
                if (SuccessFunction != null)
                {
                    SuccessFunction();
                }
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine(exp.Message);
            }
        }
        public static async void TryTaskRun<DataType>(Func<DataType> TryFunction, Action<DataType> SuccessFunction)
        {
            try
            {
                DataType resultFromTry = default(DataType);
                if (TryFunction != null)
                {
                    resultFromTry = await Task.Run(TryFunction);
                }
                if (SuccessFunction != null)
                {
                    SuccessFunction(resultFromTry);
                }
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine(exp.Message);
            }
        }
        public static async void TryTaskRun(Action TryFunction, Action<Exception> CatchFunction, Action FinallyFunction)
        {
            try
            {
                if (TryFunction != null)
                    await Task.Run(TryFunction);
            }
            catch (Exception exp)
            {
                if (CatchFunction != null)
                    CatchFunction(exp);
            }
            finally
            {
                if (FinallyFunction != null)
                {
                    FinallyFunction();
                }
            }
        }
        public static async void TryTaskRun(Action TryFunction, Action SuccessFunction, Action<Exception> CatchFunction, Action FinallyFunction)
        {
            try
            {
                if (TryFunction != null)
                    await Task.Run(TryFunction);
                if (SuccessFunction != null)
                    SuccessFunction();
            }
            catch (Exception exp)
            {
                if (CatchFunction != null)
                    CatchFunction(exp);
            }
            finally
            {
                if (FinallyFunction != null)
                {
                    FinallyFunction();
                }
            }
        }
        public static async void TryTaskRun<DataType>(Func<DataType> TryFunction, Action<DataType> SuccessFunction, Action<Exception> CatchFunction, Action FinallyFunction)
        {
            DataType result = default(DataType);
            try
            {
                if (TryFunction != null)
                    result = await Task.Run(TryFunction);
                if (SuccessFunction != null)
                    SuccessFunction(result);
            }
            catch (Exception exp)
            {
                if (CatchFunction != null)
                    CatchFunction(exp);
            }
            finally
            {
                if (FinallyFunction != null)
                {
                    FinallyFunction();
                }
            }
        }


        public static async void TryCatch(Func<Task> TryFunction, Action SuccessFunction)
        {
            try
            {
                if (TryFunction != null)
                {
                    await TryFunction();
                }
                if (SuccessFunction != null)
                {
                    SuccessFunction();
                }
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine(exp.Message);
            }
        }
        public static async void TryCatch<DataType>(Func<Task<DataType>> TryFunction, Action<DataType> SuccessFunction)
        {
            DataType valueFromTry = default(DataType);
            try
            {
                if (TryFunction != null)
                {
                    valueFromTry = await TryFunction();
                }
                if (SuccessFunction != null)
                {
                    SuccessFunction(valueFromTry);
                }
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine(exp.Message);
            }
        }
        public static async void TryCatch(Func<Task> TryFunction, Action<Exception> CatchFunction, Action FinallyFunction)
        {
            try
            {
                if (TryFunction != null)
                {
                    await TryFunction();
                }
            }
            catch (Exception exp)
            {
                if (CatchFunction != null)
                    CatchFunction(exp);
            }
            finally
            {
                if (FinallyFunction != null)
                {
                    FinallyFunction();
                }
            }
        }
        public static async void TryCatch(Func<Task> TryFunction, Action SuccessFunction, Action<Exception> CatchFunction, Action FinallyFunction)
        {
            try
            {
                if (TryFunction != null)
                {
                    await TryFunction();
                }
                if (SuccessFunction != null)
                {
                    SuccessFunction();
                }
            }
            catch (Exception exp)
            {
                if (CatchFunction != null)
                    CatchFunction(exp);
            }
            finally
            {
                if (FinallyFunction != null)
                {
                    FinallyFunction();
                }
            }
        }
        public static async void TryCatch<DataType>(Func<Task<DataType>> TryFunction, Action<DataType> SuccessFunction, Action<Exception> CatchFunction, Action FinallyFunction)
        {
            DataType valueFromTry = default(DataType);
            try
            {
                if (TryFunction != null)
                {
                    valueFromTry = await TryFunction();
                }
                if (SuccessFunction != null)
                {
                    SuccessFunction(valueFromTry);
                }
            }
            catch (Exception exp)
            {
                if (CatchFunction != null)
                    CatchFunction(exp);
            }
            finally
            {
                if (FinallyFunction != null)
                {
                    FinallyFunction();
                }
            }
        }


        public static async void TryCatch<DataType>(Func<Task<DataType>> TryFunction, Action<DataType, Exception> CatchFunction, Action<DataType> FinallyFunction)
            where DataType : class
        {
            DataType valueFromTry = default(DataType);
            try
            {
                if (TryFunction != null)
                {
                    valueFromTry = await TryFunction();
                }
            }
            catch (Exception exp)
            {
                if (CatchFunction != null)
                    CatchFunction(valueFromTry, exp);
            }
            finally
            {
                if (FinallyFunction != null)
                {
                    FinallyFunction(valueFromTry);
                }
            }
        }
        public static async void TryCatch<DataType>(Func<Task<DataType>> TryFunction, Action<DataType, Exception> CatchFunction, Action<bool, DataType> FinallyFunction)
        {
            DataType valueFromTry = default(DataType);
            bool isTryOK = true;
            try
            {
                if (TryFunction != null)
                {
                    valueFromTry = await TryFunction();
                }
            }
            catch (Exception exp)
            {
                if (CatchFunction != null)
                    CatchFunction(valueFromTry, exp);
                isTryOK = false;
            }
            finally
            {
                if (FinallyFunction != null)
                {
                    FinallyFunction(isTryOK, valueFromTry);
                }
            }
        }
        public static async void TryCatch<DataType>(Func<Task<DataType>> TryFunction, Action<Exception> CatchFunction, Action<bool, DataType> FinallyFunction)
        {
            DataType valueFromTry = default(DataType);
            bool isTryOK = true;
            try
            {
                if (TryFunction != null)
                {
                    valueFromTry = await TryFunction();
                }
            }
            catch (Exception exp)
            {
                if (CatchFunction != null)
                    CatchFunction(exp);
                isTryOK = false;
            }
            finally
            {
                if (FinallyFunction != null)
                {
                    FinallyFunction(isTryOK, valueFromTry);
                }
            }
        }
    }

这篇关于C# 通用Try, Run的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!