如何进行C#实现AOP微型框架基础的分析

发布时间:2021-11-23 23:24:12 作者:柒染
阅读:200
开发者专用服务器限时活动,0元免费领! 查看>>

如何进行C#实现AOP微型框架基础的分析,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

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

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

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

1. AopProxyAttribute AOP代理特性

  1. using System;  

  2. using System.Runtime.Remoting ;  

  3. using System.Runtime.Remoting.Proxies ;  

  4. namespace EnterpriseServerBase.Aop  

  5. {  

  6. /// <summary> 

  7. /// AopProxyAttribute  

  8. /// AOP代理特性,如果一个类想实现具体的AOP,
    只要实现AopProxyBase和IAopProxyFactory,然后加上该特性即可。  

  9. /// 2005.04.11  

  10. /// </summary> 

  11. [AttributeUsage(AttributeTargets.Class ,AllowMultiple = false)]  

  12. public class AopProxyAttribute : ProxyAttribute  

  13. {  

  14. private IAopProxyFactory proxyFactory = null ;  

  15. public AopProxyAttribute(Type factoryType)  

  16. {  

  17. this.proxyFactory = (IAopProxyFactory)Activator.CreateInstance(factoryType) ;  

  18. }  

  19. #region CreateInstance  

  20. /// <summary> 

  21. /// 获得目标对象的自定义透明代理  

  22. /// </summary> 

  23. public override MarshalByRefObject CreateInstance(Type serverType)

  24. //serverType是被AopProxyAttribute修饰的类  

  25. {  

  26. //未初始化的实例的默认透明代理  

  27. MarshalByRefObject target = base.CreateInstance (serverType);

  28. //得到位初始化的实例(ctor未执行)  

  29. object[] args = {target ,serverType} ;  

  30. //AopProxyBase rp = (AopProxyBase)Activator.CreateInstance(this.realProxyType ,args) ; 

  31. //Activator.CreateInstance在调用ctor时通过了代理,所以此处将会失败  

  32. //得到自定义的真实代理  

  33. AopProxyBase rp = this.proxyFactory.CreateAopProxyInstance(target ,serverType) ;

  34. //new AopControlProxy(target ,serverType) ;  

  35. return (MarshalByRefObject)rp.GetTransparentProxy() ;  

  36. }  

  37. #endregion  

  38. }  

2 .MethodAopSwitcherAttribute.cs

  1. using System;  

  2. namespace EnterpriseServerBase.Aop  

  3. {  

  4. /// <summary> 

  5. /// MethodAopSwitcherAttribute 
    用于决定一个被AopProxyAttribute修饰的class的某个特定方法是否启用截获 。  

  6. /// 创建原因:绝大多数时候我们只希望对某个类的一部分Method而不是所有Method使用截获。  

  7. /// 使用方法:如果一个方法没有使用MethodAopSwitcherAttribute
    特性或使用MethodAopSwitcherAttribute(false)修饰,  

  8. ///  都不会对其进行截获。只对使用了MethodAopSwitcherAttribute(true)启用截获。  

  9. /// 2005.05.11  

  10. /// </summary> 

  11. [AttributeUsage(AttributeTargets.Method ,AllowMultiple = false )]  

  12. public class MethodAopSwitcherAttribute : Attribute  

  13. {  

  14. private bool useAspect = false ;  

  15. public MethodAopSwitcherAttribute(bool useAop)  

  16. {  

  17. this.useAspect = useAop ;  

  18. }  

  19. public bool UseAspect  

  20. {  

  21. get  

  22. {  

  23. return this.useAspect ;  

  24. }  

  25. }  

  26. }  

看完上述内容,你们掌握如何进行C#实现AOP微型框架基础的分析的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:
  1. Spring 框架基础(04):AOP切面编程概念,几种实现方式演示
  2. 如何理解spring AOP 框架

开发者交流群:

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

aop

上一篇:C#如何实现前台与后台方法互调

下一篇:c语言怎么实现含递归清场版扫雷游戏

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
开发者交流群×