引言
假如现在我们有这样在这个示例中我将使用尽可能简单的逻辑实现所有功能需求,这将更突出我们所要解决的核心问题。例子是一个简单计算器类:
public class Calculator
"Add(", x, y);
int result = x + y;
Console.WriteLine(" = ", result);
return result;
"Add(", x, y);
DateTime TimeBegin = System.DateTime.Now;
int result = x + y;
TimeSpan TimeInter =System.DateTime.Now-TimeBegin;
Console.Write(" [", TimeInter);
Console.WriteLine(" = ", result);
return result;
"Add(", x, y);
DateTime TimeBegin = System.DateTime.Now;
int result = x + y;
TimeSpan TimeInter =System.DateTime.Now-TimeBegin;
Console.Write(" [", TimeInter);
Console.WriteLine(" = ", result);
return result;
"Subtract(", x, y);
DateTime TimeBegin = System.DateTime.Now;
int result = x - y;
TimeSpan TimeInter =System.DateTime.Now-TimeBegin;
Console.Write(" [", TimeInter);
Console.WriteLine(" = ", result);
return result;
"Number(", x, y);
// " 运行时间[", TimeInter);
// Console.WriteLine(" 运行结果= ", result);
// "Number(", x, y);
" 运行时间[", TimeInter);
Console.WriteLine(" 运行结果= ", result);
" 运行时间[", TimeInter);
Console.WriteLine(" 运行结果= ", returnValue);
base.PostProceed(invocation, ref returnValue, arguments);
"Number(", 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;
}
}
}
4、使用Castle.DynamicProxy调用
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);
假如现在我们有这样在这个示例中我将使用尽可能简单的逻辑实现所有功能需求,这将更突出我们所要解决的核心问题。例子是一个简单计算器类:
public class Calculator
"Add(", x, y);
int result = x + y;
Console.WriteLine(" = ", result);
return result;
"Add(", x, y);
DateTime TimeBegin = System.DateTime.Now;
int result = x + y;
TimeSpan TimeInter =System.DateTime.Now-TimeBegin;
Console.Write(" [", TimeInter);
Console.WriteLine(" = ", result);
return result;
"Add(", x, y);
DateTime TimeBegin = System.DateTime.Now;
int result = x + y;
TimeSpan TimeInter =System.DateTime.Now-TimeBegin;
Console.Write(" [", TimeInter);
Console.WriteLine(" = ", result);
return result;
"Subtract(", x, y);
DateTime TimeBegin = System.DateTime.Now;
int result = x - y;
TimeSpan TimeInter =System.DateTime.Now-TimeBegin;
Console.Write(" [", TimeInter);
Console.WriteLine(" = ", result);
return result;
"Number(", x, y);
// " 运行时间[", TimeInter);
// Console.WriteLine(" 运行结果= ", result);
// "Number(", x, y);
" 运行时间[", TimeInter);
Console.WriteLine(" 运行结果= ", result);
" 运行时间[", TimeInter);
Console.WriteLine(" 运行结果= ", returnValue);
base.PostProceed(invocation, ref returnValue, arguments);
"Number(", 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;
}
}
}
4、使用Castle.DynamicProxy调用
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);
实现过程:首先通过代码生成完成一个代理类,该代理类继承自要织入的类。然后在代理类中覆盖要拦截的方法,并在覆盖的方法中封装Invocation对象,并传给用户传入的Intercepter对象的Intercept方法。在Intercept方法依次调用Intercepter的PreProcess,通过Invocation传入的Delegate指向的回调函数,Intercepter的PostProcess方法,从而达到拦截的目的。
意义
在aop领域 可以将日志,事务,缓存等附加功能用此实现。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“关于.NET动态代理的介绍和应用简介”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。