标签:IInvocation DynamicProxy IInterceptor int void class invocation Castle public
参考: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;
标签:IInvocation,DynamicProxy,IInterceptor,int,void,class,invocation,Castle,public 来源: https://www.cnblogs.com/bruce1992/p/15350544.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。