C # extension method applied to the try Catch finally encapsulation

Source: Internet
Author: User
Tags finally block try catch

This article describes how to use the extension method to simplify the try Catch finally statement block into the following invocation form:

         Public void Test1 ()        {            new  Employee ();             = p.work ())                = handleexception (e))                = p.rest ());        

While this doesn't seem to save too much code, it looks a bit more neat. Here's how to do this:

A. Try

 Public classTryunit<t>whereT:class        {             PublicTryunit (T obj, action<t>action) {                 This. OBJ =obj;  This. Action =Action; }             PublicT OBJ {Get;Private Set; }  PublicAction<t> Action {Get;Private Set; } } Public StaticTryunit<t> try<t> ( ThisT obj, action<t> Action)whereT:class        {            return NewTryunit<t>(obj, action); }

You first define a tryunit generic class that stores the call object of the try and the method that you want to call, and then extends a try method for the object, returning a Tryunit object.

Two. catch

 Public classCatchunit<t>whereT:class        {             PublicCatchunit (tryunit<t> tryunit, action<exception>exaction) {                 This. OBJ =Tryunit.obj;  This. Action =tryunit.action;  This. Exaction =exaction; }             PublicT OBJ {Get;Private Set; }  PublicAction<t> Action {Get;Private Set; }  PublicAction<exception> exaction {Get;Private Set; } } Public StaticCatchunit<t> catch<t> ( ThisTryunit<t> Tryunit, action<exception> exaction)whereT:class        {            return NewCatchunit<t>(Tryunit, exaction); }

Similar to the practice of try, define a Catchunit class that has an action for exception handling more than Tryunit, and then extends a catch method for the Tryunit object, returning a Catchunit object. That is, after the object calls the Try method to return Tryunit, the Catch method can continue to be called, and must be called sequentially. The try and catch are actually passing arguments, and the execution of the method is deferred to finally.

Three. Finally

 Public Static voidFinally<t> ( ThisTryunit<t> tryunit)whereT:class        {            Try{tryunit.action (tryunit.obj); }            finally            {            }        }         Public Static voidFinally<t> ( ThisCatchunit<t> catchunit)whereT:class        {            Try{catchunit.action (catchunit.obj); }            Catch(Exception e) {catchunit.exaction (e); }            finally            {            }        }         Public Static voidFinally<t> ( ThisTryunit<t> Tryunit, action<t> Action)whereT:class        {            Try{tryunit.action (tryunit.obj); }            finally{action (tryunit.obj); }        }         Public Static voidFinally<t> ( ThisCatchunit<t> Catchunit, action<t> Action)whereT:class        {            Try{catchunit.action (catchunit.obj); }            Catch(Exception e) {catchunit.exaction (e); }            finally{action (catchunit.obj); }        }

The finally method can derive 4 overloaded versions, depending on whether the exception-handling block is included, or whether the operation is performed in a finally block. The complete try Catch Finally combination method is an extension of the Catchunit method, called when the Catch method is called to return a Catchunit object, or, if there is no exception handling block, extends directly from the return type Tryunit of the try method to only try Finally the combined method. Even if no processing is done in the finally block, the finally method needs to be called, because all processing is deferred to the finally call.

Well, the code is introduced here, this is my first article in the blog Park, there is a bad place to write to everyone to forgive, and welcome comments, O (∩_∩) o Thank you.

C # extension method applied to the try Catch finally encapsulation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.