温馨提示×

温馨提示×

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

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

C#怎么实现AOP微型框架

发布时间:2021-07-16 11:05:33 阅读:180 作者:chen 栏目:编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

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

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

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

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

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

using System;  using System.Runtime.Remoting.Messaging ;  namespace EnterpriseServerBase.Aop  {  /// <summary> /// IAopOperator AOP操作符接口,包括前处理和后处理  /// 2005.04.12  /// </summary> public interface IAopOperator  {  void PreProcess(IMessage requestMsg ) ;  void PostProcess(IMessage requestMsg ,IMessage Respond) ;  }  /// <summary> /// IAopProxyFactory 用于创建特定的Aop代理的实例,IAopProxyFactory的作用是使AopProxyAttribute独立于具体的AOP代理类。  /// </summary> public interface IAopProxyFactory  {  AopProxyBase CreateAopProxyInstance(MarshalByRefObject obj ,Type type) ;  }  } 

2. AopProxyBase AOP代理的基类

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

using System;  using System.Runtime.Remoting ;  using System.Runtime.Remoting.Proxies ;  using System.Runtime.Remoting.Messaging ;  using System.Runtime.Remoting.Services ;  using System.Runtime.Remoting.Activation ;  namespace EnterpriseServerBase.Aop  {  /// <summary> /// AopProxyBase 所有自定义AOP代理类都从此类派生,覆写IAopOperator接口,实现具体的前/后处理 。  /// 2005.04.12  /// </summary> public abstract class AopProxyBase : RealProxy ,IAopOperator  {  private readonly MarshalByRefObject target ; //默认透明代理  public AopProxyBase(MarshalByRefObject obj ,Type type) :base(type)  {  this.target = obj ;  }  #region Invoke  public override IMessage Invoke(IMessage msg)  {  bool useAspect = false ;  IMethodCallMessage call = (IMethodCallMessage)msg ;  //查询目标方法是否使用了启用AOP的MethodAopSwitcherAttribute  foreach(Attribute attr in call.MethodBase.GetCustomAttributes(false))  {  MethodAopSwitcherAttribute mehodAopAttr = attr as MethodAopSwitcherAttribute ;  if(mehodAopAttr != null)  {  if(mehodAopAttr.UseAspect)  {  useAspect = true ;  break ;  }  }  }  if(useAspect)  {  this.PreProcess(msg) ;  }  //如果触发的是构造函数,此时target的构建还未开始  IConstructionCallMessage ctor = call as IConstructionCallMessage ;  if(ctor != null)  {  //获取***层的默认真实代理  RealProxy default_proxy = RemotingServices.GetRealProxy(this.target) ;  default_proxy.InitializeServerObject(ctor) ;  MarshalByRefObject tp = (MarshalByRefObject)this.GetTransparentProxy() ; //自定义的透明代理 this  return EnterpriseServicesHelper.CreateConstructionReturnMessage(ctor,tp);  }  IMethodReturnMessage result_msg = RemotingServices.ExecuteMessage(this.target ,call) ; //将消息转化为堆栈,并执行目标方法,方法完成后,再将堆栈转化为消息  if(useAspect)  {  this.PostProcess(msg ,result_msg) ;  }  return result_msg ;  }  #endregion  #region IAopOperator 成员  public abstract void PreProcess(IMessage requestMsg) ;  public abstract void PostProcess(IMessage requestMsg, IMessage Respond) ;  #endregion  }  } 

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

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

向AI问一下细节

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

AI

开发者交流群×