C # create a message Interceptor (intercept) 4

Source: Internet
Author: User

C # create a message Interceptor (intercept) 4

Okay. The Interceptor is basically constructed. Let's show you how to use it.

Pay attention to one problem. If we want to intercept the object interceptor, we have to do something on the class to intercept.

First, create the class to be intercepted.

Then, we will package the class accordingly:

1. This class should mark the InterceptAttribute.

2. This class inherits ContextBoundObject. Only the ContextBoundObject class can be inherited by vs to know that the class needs to access Context, so that the marked InterceptAttribute is valid.

///     /// If you want to add the interceptpool on this class , the class need to do:    /// 1、inherited form ContextBoundObject.    /// 2、mark the InterceptAttribute.    ///     [Intercept]    public class SimonDemo:ContextBoundObject    {        public SimonDemo()        {            Console.WriteLine(" Call 'SimonDemo' - 'Constructor'  ");        }        public void Operate1()        {            Console.WriteLine("Call 'SimonDemo' - 'Operate1' ");        }    }

Then, we create an object of this class in the Main function and call the method.

 class Program    {        static void Main(string[] args)        {            Console.WriteLine("Call Main ..");            SimonDemo simon = new SimonDemo();            simon.Operate1();            Console.WriteLine("exit Main ..");            Console.Read();        }    }

In this way, through debugging, we can see what the interceptor intercepts


The following is the running result: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140729/20140729092457304.jpg" alt = "">

In this way, we can see that my program intercepts and outputs The Name Of The called function.

Only one method is provided here, and other methods of use are to be studied.

I have finished implementing the Interceptor. The younger brother understands this. If there is any error, please leave a message to the experts.


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.