zoukankan      html  css  js  c++  java
  • 如何在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;

  • 相关阅读:
    从上往下,从左往右
    idea jar包破解
    StarUML 2下载、安装、破解全过程
    windows10 找回windows照片查看器的方法
    Tomcat报错Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
    Java基于SSM在线学习系统设计与实现
    多个iframe,删除详情页时刷新同级iframe的table list
    Eclipse项目工程导入到IDEA继续开发-超详细
    MySQL用B+树做索引
    彻底禁用Chrome插件停用开发者模式提示插件version.dll
  • 原文地址:https://www.cnblogs.com/bruce1992/p/15350544.html
Copyright © 2011-2022 走看看