温馨提示×

温馨提示×

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

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

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

发布时间:2021-11-23 23:24:12 阅读:198 作者:柒染 栏目:编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

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

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

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

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

1. AopProxyAttribute AOP代理特性

using System;  using System.Runtime.Remoting ;  using System.Runtime.Remoting.Proxies ;  namespace EnterpriseServerBase.Aop  {  /// <summary> /// AopProxyAttribute  /// AOP代理特性,如果一个类想实现具体的AOP,只要实现AopProxyBaseIAopProxyFactory,然后加上该特性即可。  /// 2005.04.11  /// </summary> [AttributeUsage(AttributeTargets.Class ,AllowMultiple = false)]  public class AopProxyAttribute : ProxyAttribute  {  private IAopProxyFactory proxyFactory = null ;  public AopProxyAttribute(Type factoryType)  {  this.proxyFactory = (IAopProxyFactory)Activator.CreateInstance(factoryType) ;  }  #region CreateInstance  /// <summary> /// 获得目标对象的自定义透明代理  /// </summary> public override MarshalByRefObject CreateInstance(Type serverType)//serverType是被AopProxyAttribute修饰的类  {  //未初始化的实例的默认透明代理  MarshalByRefObject target = base.CreateInstance (serverType);//得到位初始化的实例(ctor未执行)  object[] args = {target ,serverType} ;  //AopProxyBase rp = (AopProxyBase)Activator.CreateInstance(this.realProxyType ,args) ; //Activator.CreateInstance在调用ctor时通过了代理,所以此处将会失败  //得到自定义的真实代理  AopProxyBase rp = this.proxyFactory.CreateAopProxyInstance(target ,serverType) ;//new AopControlProxy(target ,serverType) ;  return (MarshalByRefObject)rp.GetTransparentProxy() ;  }  #endregion  }  } 

2 .MethodAopSwitcherAttribute.cs

using System;  namespace EnterpriseServerBase.Aop  {  /// <summary> /// MethodAopSwitcherAttribute 用于决定一个被AopProxyAttribute修饰的class的某个特定方法是否启用截获 。  /// 创建原因:绝大多数时候我们只希望对某个类的一部分Method而不是所有Method使用截获。  /// 使用方法:如果一个方法没有使用MethodAopSwitcherAttribute特性或使用MethodAopSwitcherAttribute(false)修饰,  ///  都不会对其进行截获。只对使用了MethodAopSwitcherAttribute(true)启用截获。  /// 2005.05.11  /// </summary> [AttributeUsage(AttributeTargets.Method ,AllowMultiple = false )]  public class MethodAopSwitcherAttribute : Attribute  {  private bool useAspect = false ;  public MethodAopSwitcherAttribute(bool useAop)  {  this.useAspect = useAop ;  }  public bool UseAspect  {  get  {  return this.useAspect ;  }  }  }  } 

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

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

向AI问一下细节

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

aop
AI

开发者交流群×