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.