ok,我们拦截器基本构造完成,接下来我来告诉大家如何去使用。
注意一个问题,object拦截器我们要拦截什么,那么我们就要在需要拦截的类上面做手脚了。
首先,创建我们需要被拦截的类。
然后,我们再对类进行相应的包装:
1、该类要标记InterceptAttribute属性
2、该类要继承ContextBoundObject,只有继承ContextBoundObject的类,vs才能知道该类需要访问Context,这样标记的InterceptAttribute才有效。
/// <summary>
    /// If you want to add the interceptpool on this class , the class need to do:
    /// 1、inherited form ContextBoundObject.
    /// 2、mark the InterceptAttribute.
    /// </summary>
    [Intercept]
    public class SimonDemo:ContextBoundObject
    {
        public SimonDemo()
        {
            Console.WriteLine(" Call 'SimonDemo' - 'Constructor'  ");
        }
        public void Operate1()
        {
            Console.WriteLine("Call 'SimonDemo' - 'Operate1' ");
        }
    } class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Call Main ..");
            SimonDemo simon = new SimonDemo();
            simon.Operate1();
            Console.WriteLine("exit Main ..");
            Console.Read();
        }
    }
接下来是运行结果:
这样可以看出我的程序拦截,并输出了调用函数的名字。
在此仅提供一种方法,其余的使用方法有待研究。
写到这里我的拦截器实现完了,小弟了解尚浅,如有错误请高手们留言指出。
C#制作一个消息拦截器(intercept)4,布布扣,bubuko.com
原文:http://blog.csdn.net/lwcbest/article/details/38230881