C#擴充方法應用之 try catch finally 封裝

來源:互聯網
上載者:User

標籤:style   blog   io   color   sp   on   div   log   bs   

      本文將介紹如何利用擴充方法將 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謝謝。

 

C#擴充方法應用之 try catch finally 封裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.