资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#怎么实现AOP微型框架

本篇内容介绍了“C#怎么实现AOP微型框架”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

成都创新互联是一家集网站建设,靖州企业网站建设,靖州品牌网站建设,网站定制,靖州网站建设报价,网络营销,网络优化,靖州网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

在向大家详细介绍C#实现AOP微型框架之前,首先让大家了解下微型框架的.cs文件,然后全面介绍C#实现AOP微型框架。

在前面的系列文章中,我介绍了消息、代理与AOP的关系,这次将我自己用C#实现AOP微型框架拿出来和大家交流一下。

AOP的最基本功能就是实现特定的预处理和后处理,我通过代理让C#实现AOP微型框架。先来看看构成此微型框架的.cs文件。

1.CommonDef.cs 用于定义最基本的AOP接口

  1. using System;  

  2. using System.Runtime.Remoting.Messaging ;  

  3.  

  4. namespace EnterpriseServerBase.Aop  

  5. {  

  6. /// 

     

  7. /// IAopOperator AOP操作符接口,包括前处理和后处理  

  8. /// 2005.04.12  

  9. ///  

  10. public interface IAopOperator  

  11. {  

  12. void PreProcess(IMessage requestMsg ) ;  

  13. void PostProcess(IMessage requestMsg ,IMessage Respond) ;  

  14. }  

  15.  

  16. /// 

     

  17. /// IAopProxyFactory 用于创建特定的Aop代理的实例,
    IAopProxyFactory的作用是使AopProxyAttribute独立于具体的AOP代理类。  

  18. ///  

  19. public interface IAopProxyFactory  

  20. {  

  21. AopProxyBase CreateAopProxyInstance(MarshalByRefObject obj ,Type type) ;  

  22. }  

  23.  

2. AopProxyBase AOP代理的基类

所有自定义AOP代理类都从此类派生,覆写IAopOperator接口,实现具体的前/后处理 。

  1. using System;  

  2. using System.Runtime.Remoting ;  

  3. using System.Runtime.Remoting.Proxies ;  

  4. using System.Runtime.Remoting.Messaging ;  

  5. using System.Runtime.Remoting.Services ;  

  6. using System.Runtime.Remoting.Activation ;  

  7.  

  8. namespace EnterpriseServerBase.Aop  

  9. {  

  10. /// 

     

  11. /// AopProxyBase 所有自定义AOP代理类都从此类派生,
    覆写IAopOperator接口,实现具体的前/后处理 。  

  12. /// 2005.04.12  

  13. ///  

  14. public abstract class AopProxyBase : RealProxy ,IAopOperator  

  15. {  

  16. private readonly MarshalByRefObject target ; //默认透明代理  

  17.  

  18. public AopProxyBase(MarshalByRefObject obj ,Type type) :base(type)  

  19. {  

  20. this.target = obj ;  

  21. }  

  22.  

  23. #region Invoke  

  24. public override IMessage Invoke(IMessage msg)  

  25. {  

  26. bool useAspect = false ;  

  27. IMethodCallMessage call = (IMethodCallMessage)msg ;  

  28.  

  29. //查询目标方法是否使用了启用AOP的MethodAopSwitcherAttribute  

  30. foreach(Attribute attr in call.MethodBase.GetCustomAttributes(false))  

  31. {  

  32. MethodAopSwitcherAttribute mehodAopAttr = attr as MethodAopSwitcherAttribute ;  

  33. if(mehodAopAttr != null)  

  34. {  

  35. if(mehodAopAttr.UseAspect)  

  36. {  

  37. useAspect = true ;  

  38. break ;  

  39. }  

  40. }  

  41. }  

  42.  

  43. if(useAspect)  

  44. {  

  45. this.PreProcess(msg) ;  

  46. }  

  47.  

  48. //如果触发的是构造函数,此时target的构建还未开始  

  49. IConstructionCallMessage ctor = call as IConstructionCallMessage ;  

  50. if(ctor != null)  

  51. {  

  52. //获取***层的默认真实代理  

  53. RealProxy default_proxy = RemotingServices.GetRealProxy(this.target) ;  

  54.  

  55. default_proxy.InitializeServerObject(ctor) ;  

  56. MarshalByRefObject tp = (MarshalByRefObject)this.GetTransparentProxy() ; 

  57. //自定义的透明代理 this  

  58.  

  59. return EnterpriseServicesHelper.CreateConstructionReturnMessage(ctor,tp);  

  60. }  

  61.  

  62. IMethodReturnMessage result_msg = RemotingServices.ExecuteMessage(this.target ,call) ; 

  63. //将消息转化为堆栈,并执行目标方法,方法完成后,再将堆栈转化为消息  

  64.  

  65. if(useAspect)  

  66. {  

  67. this.PostProcess(msg ,result_msg) ;  

  68. }  

  69.  

  70. return result_msg ;  

  71.  

  72. }  

  73. #endregion  

  74.  

  75. #region IAopOperator 成员  

  76.  

  77. public abstract void PreProcess(IMessage requestMsg) ;  

  78. public abstract void PostProcess(IMessage requestMsg, IMessage Respond) ;  

  79. #endregion  

  80. }  

“C#怎么实现AOP微型框架”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


分享名称:C#怎么实现AOP微型框架
当前链接:http://cdkjz.cn/article/ggcgho.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220