国产探花免费观看_亚洲丰满少妇自慰呻吟_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

 }

}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凉城县| 巴林左旗| 怀仁县| 万源市| 石柱| 禹城市| 河池市| 武平县| 横山县| 衡阳县| 泽州县| 富锦市| 通河县| 诸城市| 奉节县| 敦煌市| 佛学| 金昌市| 民丰县| 萝北县| 南阳市| 建德市| 乌拉特后旗| 定兴县| 温宿县| 酉阳| 偏关县| 云霄县| 新龙县| 东阿县| 象州县| 青川县| 兴仁县| 兴海县| 丁青县| 吴旗县| 星子县| 尼木县| 兰溪市| 定南县| 探索|