C#扩展方法应用之 try catch finally 封装
本文将介绍如何利用扩展方法将 try catch finally 语句块简化成如下的调用形式:
public void Test1(){Employee emp = new Employee();emp.Try(p => p.Work()).Catch(e => HandleException(e)).Finally(p => p.Rest());}
虽然这样做似乎节省不了太多代码,但看起来更工整一点。下面介绍如何实现:
一. try
public class TryUnit<T> where T : class{public TryUnit(T obj, Action<T> action){this.Obj = obj;this.Action = action;}public T Obj { get; private set; }public Action<T> Action { get; private set; }}public static TryUnit<T> Try<T>(this T obj, Action<T> action) where T : class{return new TryUnit<T>(obj, action);}
首先定义一个TryUnit泛型类,用来存储try的调用对象和想调用的方法,然后为对象扩展一个Try方法,返回一个TryUnit对象。
二. catch
public class CatchUnit<T> where T : class{public CatchUnit(TryUnit<T> tryUnit, Action<Exception> exAction){this.Obj = tryUnit.Obj;this.Action = tryUnit.Action;this.ExAction = exAction;}public T Obj { get; private set; }public Action<T> Action { get; private set; }public Action<Exception> ExAction { get; private set; }}public static CatchUnit<T> Catch<T>(this TryUnit<T> tryUnit, Action<Exception> exAction) where T : class{return new CatchUnit<T>(tryUnit, exAction);}
与try的做法类似,再定义一个CatchUnit类,它比TryUnit多出一个对异常处理的Action;然后为TryUnit对象扩展一个Catch方法,返回一个CatchUnit对象。也就是说,在对象调用了Try方法返回TryUnit之后,才可以继续调用Catch方法,必须按顺序调用。Try和Catch实际上都是在传递参数,方法的执行将会延迟到Finally中。
三. finally
public static void Finally<T>(this TryUnit<T> tryUnit) where T : class{try{tryUnit.Action(tryUnit.Obj);}finally{}}public static void Finally<T>(this CatchUnit<T> catchUnit) where T : class{try{catchUnit.Action(catchUnit.Obj);}catch (Exception e){catchUnit.ExAction(e);}finally{}}public static void Finally<T>(this TryUnit<T> tryUnit, Action<T> action) where T : class{try{tryUnit.Action(tryUnit.Obj);}finally{action(tryUnit.Obj);}}public static void Finally<T>(this CatchUnit<T> catchUnit, Action<T> action) where T : class{try{catchUnit.Action(catchUnit.Obj);}catch (Exception e){catchUnit.ExAction(e);}finally{action(catchUnit.Obj);}}
Finally方法根据是否包括异常处理块,finally块中是否执行操作可派生出4个重载版本。完整的 try catch finally 组合方法是对CatchUnit的扩展方法,在调用Catch方法返回CatchUnit对象时调用;如果没有异常处理块,则直接从try方法的返回类型TryUnit上扩展出只有 try finally 的组合方法。即使finally块中不做任何处理,也需要调用Finally方法,因为所有处理都被延迟到了Finally中调用。
好了,代码就介绍到这里,这是本人在博客园的第一篇文章,有写的不好的地方望大家原谅,并欢迎提出意见,O(∩_∩)O谢谢。
转载于:https://www.cnblogs.com/enterframe/p/4164758.html
C#扩展方法应用之 try catch finally 封装相关推荐
- 使用扩展方法对代码的行为进行封装的例子:封装UIElement的“拖动”
很多情况下,我们需要对界面上的元素进行拖动,用鼠标在VS中biaji,biaji,biaji,点几个事件,然后再写出一堆代码,浪费时间不说,由IDE自动生成的那些代码实在是太难看,影响心情.本文使用扩 ...
- 您最喜欢的C#扩展方法是什么? (codeplex.com/extensionoverflow)
让我们列出一个答案列表,在其中发布优秀和最喜欢的扩展方法 . 要求是必须张贴完整的代码,并提供示例和使用说明. 基于对该主题的高度兴趣,我在Codeplex上建立了一个名为extensionoverf ...
- 再不用担心DataRow类型转换和空值了(使用扩展方法解决高频问题)
在使用DataRow读取数据时,通常会遇到数据可能为Null, 但是又需要转换为如int等其它类型的数据,因此就通常会写这样的代码: if (dr[name] != DBNull.Value & ...
- jQuery Validate 提交表单验证失败扩展方法
由于Validate没有提供表单提交过后,验证不通过触发方法.这里做一下扩展. 引用场景:每次提交表单元素验证不通过触发方法 打开源代码 找到focusInvalid 方法, 这里是提交表单时验证不通 ...
- Linq 下的扩展方法太少了,您期待的 MoreLinq 来啦
一:背景 1. 讲故事 前几天看同事在用 linq 给内存中的两个 model 做左连接,用过的朋友都知道,你一定少不了一个叫做 DefaultIfEmpty 函数,这玩意吧,本来很流畅的 from. ...
- 给微软的依赖注入框架写一些扩展方法
给微软的依赖注入框架写一些扩展方法 Intro 现在在项目里大多都是直接使用微软的依赖注入框架,而微软的注入方式比较简单,不如 AutoFac 使用起来灵活,于是想给微软的依赖注入增加一些扩展,使得可 ...
- ASP.Net string 类的扩展方法 [转]
string 类的扩展方法列表(基本相同于 IEnumerable<T> 接口的成员列表): Aggregate<> //累加 All<> //是否都满足条件 An ...
- c# 扩展方法奇思妙用高级篇五:ToString(string format) 扩展
在.Net中,System.Object.ToString()是用得最多的方法之一,ToString()方法在Object类中被定义为virtual,Object类给了它一个默认实现: 1 p ...
- string 类的扩展方法
string 类的扩展方法 分类: 2011年技术文章2011-05-11 18:14 76人阅读 评论(0) 收藏 举报 string 类的扩展方法列表(基本相同于 IEnumerable<T ...
最新文章
- 扫盲文章:AMF,RTMP,RTMPT,RTMPS
- JS 原生实现复选框全选反选功能
- 阿里云服务器(BT面板)Vue+Node(Egg)部署流程
- C# asp:Repeater DataSource ListT
- Spring Boot(十二)单元测试JUnit
- 【DP】剪草(jzoj 1510)
- SQL Performance Analyzer SPA常用脚本汇总
- 2012禁用ip隧道 win_Windows 7下关闭IPV6隧道的技巧方法
- Go本地浏览Web服务器
- 网络热词下的民意传播
- 2019湖南多校第四场
- 阿里云徐栋:“下一代互联网”三大技术趋势
- 一套SpringBoot+VUE全平台支付系统源码
- 1960-2019年世界各国人均GDP增长率
- 判断table中的tbody是否有数据
- Android自定义View——仿QQ等级天数进度
- 了解一下Windows Cracker
- 水下航行器简介及水下面临的挑战
- setInterval()与setTimeout() 详细
- 做人有弹性,做事有韧性
热门文章
- NOCOUNT如何影响ADO.NET(SET NOCOUNT ON的性能问题)
- from advanced computing to machine learning
- how to covid free at cambridge?
- building a software for what?
- get your subscription available at the moments on bilibili
- 算法笔记 1 31 chapter4
- 【l转】VS2015下解决:无法解析的外部符号 __imp___vsnprintf 及__iob_func
- 查看linux是几位的操作系统
- Function接口 – Java8中java.util.function包下的函数式接口
- Java BIO、NIO、AIO 学习