在基于前两篇文章给出的aop微型框架的实现的基础上,我们可以写个例子来测试一下了:)
public class aopcontrolproxyfactory : iaopproxyfactory
{
#region iaopproxyfactory 成员
public aopproxybase createaopproxyinstance(marshalbyrefobject obj, type type)
{
return new aopcontrolproxy(obj ,type) ;
}
#endregion
}
//自定义真实代理
public class aopcontrolproxy : aopproxybase
{
public aopcontrolproxy(marshalbyrefobject obj ,type type) : base(obj ,type)
{
}
public override void preprocess(imessage requestmsg)
{
console.write("begin aop !") ;
}
public override void postprocess(imessage requestmsg, imessage respond)
{
console.write("end aop !") ;
}
}
[aopproxyattribute(typeof(aopcontrolproxyfactory))] //将自己委托给aop代理aopcontrolproxy
public class example : contextboundobject//放到特定的上下文中,该上下文外部才会得到该对象的透明代理
{
private string name ;
public example(string a)
{
this.name = a ;
}
[methodaopswitcherattribute(true)]
public void say_hello()
{
console.writeline("hello ! " + name ) ;
}
public void saybyebye()
{
console.writeline("bye ! " + name ) ;
}
}
新闻热点
疑难解答