天天看點

MSBuild + MSILInect實作編譯時AOP之預覽

Class Diagram:

MSBuild + MSILInect實作編譯時AOP之預覽

1:IMethodInject:Interface,擁有Executeing,Exceptioned,ExecuteSuccess三個契約為别為執行前,異常,成功。它們都有公同的參數類型:MethodExecutionEventArgs

MSBuild + MSILInect實作編譯時AOP之預覽

    Executeing:傳回值為bool類型,将決定是否繼續執行方法體。Exceptioned:屬性Eeption代表發生的異常資訊,傳回值ExceptionStrategy(取值:Handle, ReThrow, ThrowNew)決定異常處理機制,Handle已處理并忽略,ReThrow重新抛出,ThrowNew抛出一個包裝後的來源于MethodExecutionEventArgs 的Exception。ExecuteSuccess,對于擁有傳回值的方法,可以修改MethodExecutionEventArgs 的ReturnValue,修改傳回值。最後MethodExecutionEventArgs的Order決定多個Attribute的注入先後,即方法截獲的先後順序。

1:MethodInterceptBase:針對于方法Attribute标簽,實作方法截獲

MSBuild + MSILInect實作編譯時AOP之預覽

View Code

2:MatchedMethodInterceptBase:和上面方法之上的MethodInterceptBase大體一緻,差別在于其應用于class之上,屬性Rule為截獲方法比對(應用于多個方法之上相同截獲),支援*比對。

MSBuild + MSILInect實作編譯時AOP之預覽

3:PropertyInterceptBase:實作屬性的注入,其屬性Action(enum PropertyInterceptAction:None Get, Set)指注入屬性的get或者Set; 

MSBuild + MSILInect實作編譯時AOP之預覽

View Code 

其上預設都是Executeing繼續執行,Exceptioned為抛出不處理,成功不修改result。

下面是一個簡單測試Code:

MSBuild + MSILInect實作編譯時AOP之預覽

 注意測試有兩種方式(由于沒有安裝包):

1:先重編譯測試項目,運作ConsoleApplication2(在屬性中修改控制台其實參數)。在檢視測試項目。

在後續将會從簡單Demo分析實作原理。

本文轉自破狼部落格園部落格,原文連結:http://www.cnblogs.com/whitewolf/archive/2011/08/09/2132217.html,如需轉載請自行聯系原作者