C# 通用Try, Run
2021/7/13 12:05:49
本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具
- 2024-01-24.NET集成IdGenerator生成分布式全局唯一ID