如何在Castle.DynamicProxy中使用IInterceptor?(How use IInterceptor in Castle.DynamicProxy?)
2021/9/28 23:11:16
本文主要是介绍如何在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;
这篇关于如何在Castle.DynamicProxy中使用IInterceptor?(How use IInterceptor in Castle.DynamicProxy?)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享