zoukankan      html  css  js  c++  java
  • 切面编程AOP之Castle.Core

    1.Nuget中搜索Castle.Core并install 

    2.创建一个普通的类(注意类中只有标记virtual才能实现拦截 )

     public class TestInterceptor
        {
            public virtual void MethodInterceptor()
            {
                Console.WriteLine("走过滤器");
            }
    
            public void NoInterceptor()
            {
                Console.WriteLine("没走过滤器");
            }
        }

    3. 创建拦截器

    public class Interceptor: StandardInterceptor
        {
            protected override void PreProceed(IInvocation invocation)
            {
                Console.WriteLine("调用前的拦截器, 方法名是: {0}", invocation.Method.Name);
            }
    
            protected override void PerformProceed(IInvocation invocation)
            {
                Console.WriteLine("拦截的方法返回时调用的拦截, 方法名是: {0}", invocation.Method.Name);
            }
    
            protected override void PostProceed(IInvocation invocation)
            {
                Console.WriteLine("调用后的拦截器, 方法名是: {0}", invocation.Method.Name);
            }
        }

    4. 控制台中调用

    static void Main(string[] args)
            {
                #region Castle.Core
                ProxyGenerator generator = new ProxyGenerator(); // 实例化代理生成器
                Interceptor interceptor = new Interceptor(); //实例化拦截器
    
                //使用代理生成器创建Person对象, 而不是使用new关键字实例化
                TestInterceptor test = generator.CreateClassProxy<TestInterceptor>(interceptor);
                Console.WriteLine("当前类型: {0}, 父类型: {1}", test.GetType(), test.GetType().BaseType);
                Console.WriteLine();
                test.MethodInterceptor();
                Console.WriteLine();
                test.NoInterceptor();
                Console.WriteLine();
                Console.ReadLine();
                #endregion
            }
    

      

  • 相关阅读:
    Datediff的使用(统计本日,昨日,本周,本月)
    数据库之查询语句
    数据库之表关系
    数据库中的数据类型
    数据库概念和基本操作
    IO模型
    并发编程之协程
    GIL与多线程(线程池与进程池,同步异步阻塞非阻塞)
    并发编程之多线程
    并发编程之多进程
  • 原文地址:https://www.cnblogs.com/zxhome/p/10730477.html
Copyright © 2011-2022 走看看