AOP之PostSharp2-OnMethodBoundaryAspect

在上一篇中我們了解了簡單的OnExceptionAspectAOP面向方向切入,在第一節(jié)中我們將繼續(xù)我們的PostSharp AOP系列的OnMethodBoundaryAspect方法行為的切入,這也是我們常用的AOP切入。

裕民網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,裕民網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為裕民成百上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請找那個(gè)售后服務(wù)好的裕民做網(wǎng)站的公司定做!

   OnMethodBoundaryAspect顧名思義其為對方法邊界的切入,定義如下:

AOP之PostSharp2-OnMethodBoundaryAspect

在這里提供了四個(gè)方法邊界點(diǎn)為我們切入。我們可以很輕松的對方法權(quán)限,執(zhí)行時(shí)間,參數(shù)合法性等aspect。

aspect傳入?yún)?shù)MethodExecutionArgs給我如下信息,同時(shí)還包括父類AdviceArgs的Instance屬性,實(shí)例方法才有值,靜態(tài)方法則為null,

AOP之PostSharp2-OnMethodBoundaryAspect

這里還需要說一下屬性FlowBehavior:表示方法執(zhí)行行為,是一個(gè)枚舉變量:

AOP之PostSharp2-OnMethodBoundaryAspect

二:執(zhí)行時(shí)間統(tǒng)計(jì)demo

下面我們實(shí)踐一個(gè)方法執(zhí)行時(shí)間統(tǒng)計(jì)demo:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using PostSharp.Aspects;  
  6.  
  7. namespace PostSharpDemo  
  8. {  
  9.     [Serializable]  
  10.     public class OnMethodBoundaryAspectDemoAttribute : OnMethodBoundaryAspect  
  11.     {  
  12.         public bool Enabled  
  13.         {  
  14.             get;  
  15.             set;  
  16.         }  
  17.  
  18.         public override void OnEntry(MethodExecutionArgs args)  
  19.         {  
  20.             if (this.Enabled)  
  21.             {  
  22.                 args.MethodExecutionTag = System.Diagnostics.Stopwatch.StartNew();  
  23.             }  
  24.         }  
  25.         public override void OnExit(MethodExecutionArgs args)  
  26.         {  
  27.             if (this.Enabled)  
  28.             {  
  29.                 var sw = args.MethodExecutionTag as System.Diagnostics.Stopwatch;  
  30.                 if (sw != null)  
  31.                 {  
  32.                     sw.Stop();  
  33.                     Console.WriteLine(String.Format("方法{0}執(zhí)行時(shí)間為:{1}s", args.Method.Name, sw.ElapsedMilliseconds / 1000));  
  34.                     sw = null;  
  35.                 }  
  36.             }  
  37.         }  
  38.     }  
  39. }  
測試方法:
 
  1. [OnMethodBoundaryAspectDemoAttribute(Enabled=true)]   
  2.        public static void OnMethodBoundaryAspectDemoAttributeTest()   
  3.        {   
  4.            System.Threading.Thread.Sleep(2000);   
  5.        } 

結(jié)果如下:

AOP之PostSharp2-OnMethodBoundaryAspect

注:這里我們也可以用到我們上節(jié)說的 多播(Multicasting)加到我們的class,assembly上統(tǒng)計(jì)我們所有的方法。

在最后在廢話一句,我們可以很輕松的指定我們的方法(比如使我們的wcf服務(wù)操作契約)的訪問權(quán)限,比如基于操作權(quán)限的功能點(diǎn)function的處理,如[PowerAttribute(“Add,Edit”)]這樣簡單處理,我們只需要在OnEnter中aspect,決定方法FlowBehavior行為,剩下的事情教給大家自己實(shí)踐。

   歡迎大家積極指正和多多交流。

附件:demo下載

其他AOP參考:

  • AOP之PostSharp初見-OnExceptionAspect
  • AOP之PostSharp2-OnMethodBoundaryAspect
  • AOP之PostSharp3-MethodInterceptionAspect
  • AOP之PostSharp4-實(shí)現(xiàn)類INotifyPropertyChanged植入
  • AOP之PostSharp5-LocationInterceptionAspect
  • http://www.cnblogs.com/whitewolf/category/312638.html

分享題目:AOP之PostSharp2-OnMethodBoundaryAspect
文章轉(zhuǎn)載:http://muchs.cn/article10/jepddo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、Google、用戶體驗(yàn)、軟件開發(fā)、ChatGPT關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)