• 如何在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;

  • 相关阅读:
    [工作中的设计模式]中介模式模式Mediator
    [工作中的设计模式]责任链模式chain
    [工作中的设计模式]迭代子模式Iterator
    [工作中的设计模式]组合模式compnent
    TI IPNC Web网页之流程分析
    TI IPNC Web网页之GoDB开发环境
    安装ubuntu时将boot目录单独挂载的意义
    ubuntu添加自定义vga输出分辨率
    GCC编译默认的头文件搜索路径
    设置搜狗输入法在任何时候按左右两侧的shift激活
  • 原文地址:https://www.cnblogs.com/bruce1992/p/15350544.html
Copyright © 2020-2023  润新知