扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:长风破浪 来源:博客园 2007年11月3日
关键字:
using System; namespace Proxy { // /// <summary> // /// CalProxy 的摘要说明。 // /// </summary> // public class CalProxy:ICalculator // { // private Calculator _Calculator; // public CalProxy() // { // this._Calculator=new Calculator(); // } // private DateTime TimeBegin = System.DateTime.Now; // private void PreDoSomething(int x, int y) // { // TimeBegin = System.DateTime.Now; // Console.Write("Number({0},{1})\n", x, y); // } // //实现add // public virtual int Add(int x, int y) // { // this.PreDoSomething(x,y); // int result = this._Calculator.Add(x,y); // this.PostDoSomething(result); // return result; // } // //实现sub // public virtual int Subtract(int x, int y) // { // this.PreDoSomething(x,y); // int result = this._Calculator.Subtract(x,y); // this.PostDoSomething(result); // return result; // } // private void PostDoSomething(int result) // { // TimeSpan TimeInter =System.DateTime.Now-TimeBegin; // Console.Write(" 运行时间[{0}]\n ", TimeInter); // Console.WriteLine(" 运行结果= {0}\n", result); // } // } /// <summary> /// CalProxy 的摘要说明。 /// </summary> public class CalProxy:Calculator { public CalProxy() {} private DateTime TimeBegin = System.DateTime.Now; private void PreDoSomething(int x, int y) { TimeBegin = System.DateTime.Now; Console.Write("Number({0},{1})\n", x, y); } //实现add public override int Add(int x, int y) { this.PreDoSomething(x,y); int result = base.Add(x,y); this.PostDoSomething(result); return result; } //实现sub public override int Subtract(int x, int y) { this.PreDoSomething(x,y); int result = base.Subtract(x,y); this.PostDoSomething(result); return result; } private void PostDoSomething(int result) { TimeSpan TimeInter =System.DateTime.Now-TimeBegin; Console.Write(" 运行时间[{0}]\n ", TimeInter); Console.WriteLine(" 运行结果= {0}\n", result); } } } |
ICalculator ICal=new Proxy.CalProxy(); ICal.Add(5,3); ICal.Subtract(7,2); |
using System; using System.Collections; using Castle.DynamicProxy; namespace Proxy { /// <summary> /// ProxyInterceptor 拦截器 实现了日志和性能监测 /// </summary> public class ProxyInterceptor:StandardInterceptor { private System.DateTime TimeBegin=System.DateTime.Now; public ProxyInterceptor() {} protected override void PostProceed(IInvocation invocation, ref object returnValue, params object[] arguments) { TimeSpan TimeInter =System.DateTime.Now-TimeBegin; Console.Write(" 运行时间[{0}]\n ", TimeInter); Console.WriteLine(" 运行结果= {0}\n", returnValue); base.PostProceed(invocation, ref returnValue, arguments); } protected override void PreProceed(IInvocation invocation, params object[] args) { Console.Write("Number({0},{1})\n", args[0], args[1]); TimeBegin=System.DateTime.Now; base.PreProceed(invocation, args); } public override object Intercept(IInvocation invocation, params object[] args) { PreProceed(invocation, args); object retValue = invocation.Proceed( args ); PostProceed(invocation, ref retValue, args); return retValue; } } } |
ProxyGenerator generator = new ProxyGenerator(); object proxy = generator.CreateClassProxy(typeof(Calculator), new ProxyInterceptor()); ICalculator ICalCastle=proxy as ICalculator; ICalCastle.Add(5,3); ICalCastle.Subtract(7,2); |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者