• 如何在Castle.DynamicProxy中使用IInterceptor?(How use IInterceptor in Castle.DynamicProxy?)


    参考:https://www.it1352.com/1796724.html

    我写了一个这样的例子

    简单计算器类:

    public class Calculator
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
    }
    

    实现了DynamicProxy提供的"IInterceptor"

     [Serializable]
    public abstract class Interceptor : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            ExecuteBefore(invocation);
            invocation.Proceed();
            ExecuteAfter(invocation);
    
        }
        protected abstract void ExecuteAfter(IInvocation invocation);
        protected abstract void ExecuteBefore(IInvocation invocation);
    }
    

    创建了一个Interceptor类,并从"Interceptor"类继承

        public class CalculatorInterceptor : Interceptor
    {
        protected override void ExecuteBefore(Castle.DynamicProxy.IInvocation invocation)
        {
            Console.WriteLine("Start");
        }
    
        protected override void ExecuteAfter(Castle.DynamicProxy.IInvocation invocation)
        {
            Console.WriteLine("End");
        }
    }
    

    但是当我使用它不起作用时!!

    static void Main(string[] args)
        {
            ProxyGenerator generator = new ProxyGenerator();
            Calculator c = generator.CreateClassProxy<Calculator>(new CalculatorInterceptor());
            var r = c.Add(11, 22);
            Console.WriteLine(r);
            Console.ReadKey();
        }
    

    我例外地看到了这样的东西:

    START
    33
    END
    

    但仅显示

    33
    

    我该如何纠正?!

    解决方案

    尝试将方法Add设为虚拟.

    public class Calculator
    {
        public virtual int Add(int a, int b)
        {
            return a + b;
        }
    }
    

    代理生成器创建一个继承Calculator的新类.因此,方法Add获得重写以使拦截成为可能.

    I wrote an example like this

    Simple Calculator class :

    public class Calculator
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
    }
    

    implemented "IInterceptor" that provided by DynamicProxy

     [Serializable]
    public abstract class Interceptor : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            ExecuteBefore(invocation);
            invocation.Proceed();
            ExecuteAfter(invocation);
    
        }
        protected abstract void ExecuteAfter(IInvocation invocation);
        protected abstract void ExecuteBefore(IInvocation invocation);
    }
    

    Created an Interceptor class and inherited from "Interceptor" class

        public class CalculatorInterceptor : Interceptor
    {
        protected override void ExecuteBefore(Castle.DynamicProxy.IInvocation invocation)
        {
            Console.WriteLine("Start");
        }
    
        protected override void ExecuteAfter(Castle.DynamicProxy.IInvocation invocation)
        {
            Console.WriteLine("End");
        }
    }
    

    but when I used it NOT working !!!

    static void Main(string[] args)
        {
            ProxyGenerator generator = new ProxyGenerator();
            Calculator c = generator.CreateClassProxy<Calculator>(new CalculatorInterceptor());
            var r = c.Add(11, 22);
            Console.WriteLine(r);
            Console.ReadKey();
        }
    

    I excepted to see something like this :

    START
    33
    END
    

    but only show

    33
    

    How I can correct it ?!

    解决方案

    Try to make the method Add virtual.

    public class Calculator
    {
        public virtual int Add(int a, int b)
        {
            return a + b;
        }
    }
    

    The proxy generator creates a new class inheriting Calculator. Thus, the method Add gets an override to make interception possible.

    获得方法名

    System.Reflection.MethodBase.GetCurrentMethod().Name;

  • 相关阅读:
    spring的bean的属性注入
    spring中bean的常用属性
    spring Code(spring 核心)
    mybatis的缓存
    mybatis 调用存储过程
    mybatis的动态sql
    mybatis的关系映射
    mybatis添加信息自动生成主键
    mybatis传递参数的方法
    如何编写跨平台的Java代码
  • 原文地址:https://www.cnblogs.com/bruce1992/p/15350544.html
Copyright © 2020-2023  润新知