在基于前两篇文章给出的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 ) ;
}
}