国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C# AOP微型框架實現(一)

2024-07-21 02:17:56
字體:
來源:轉載
供稿:網友


收集最實用的網頁特效代碼!

 

在前面的系列文章中,我介紹了消息、代理與aop的關系,這次將我自己實現的一個aop微型框架拿出來和大家交流一下。

aop的最基本功能就是實現特定的預處理和后處理,我通過代理實現了此微型框架。

先來看看構成此微型框架的4個.cs文件。

1.commondef.cs 用于定義最基本的aop接口

/************************************* commondef.cs **************************

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

 }

}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 获嘉县| 建瓯市| 固原市| 鱼台县| 沙田区| 拉萨市| 昆明市| 台江县| 靖西县| 正定县| 安仁县| 额尔古纳市| 内乡县| 泾川县| 故城县| 宁海县| 荔波县| 岐山县| 修文县| 江油市| 洛川县| 桂阳县| 宜兴市| 松原市| 西宁市| 瓮安县| 固始县| 苍南县| 晋宁县| 长汀县| 阿瓦提县| 九江县| 延川县| 固阳县| 青神县| 托克逊县| 汉中市| 邵东县| 新宁县| 通河县| 新晃|