MSBuild + MSILInect实现编译时AOP-改变前后对比

 实现静态AOP,就需要我们在预编译时期,修改IL实现对代码逻辑的修改。Mono.Cecil就是一个很好的IL解析和注入框架,参见 编译时MSIL注入--实践Mono Cecil(1)

  我的思路为:在编译时将加有继承制MethodInterceptBaseAttribute标签的原方法,重新组装成一个方法(并加上[CompilerGenerated]标签),在加入横切注入接口前后代码,调用此方法。

比如代码:

[TestAOPAttribute(Order  =   1 )]

public  Class1 TestMethod1( int  i,  int  j, Class1 c) 
        { 
            Console.WriteLine(
" ok " ); 
            
return   new  Class1(); 
        }

public   class  TestAOPAttribute : Green.AOP.MethodInterceptBase 
    { 

        
#region  IMethodInject Members 

        
public   override   bool  Executeing(Green.AOP.MethodExecutionEventArgs args) 
        { 
            Console.WriteLine(
this .GetType()  +   " : "   +   " Executeing " ); 
            
return   true
        } 

        
public   override  Green.AOP.ExceptionStrategy Exceptioned(Green.AOP.MethodExecutionEventArgs args) 
        { 
            Console.WriteLine(
this .GetType()  +   " : "   +   " Exceptioned " ); 
            
return  Green.AOP.ExceptionStrategy.Handle; 
        } 

        
public   override   void  ExecuteSuccess(Green.AOP.MethodExecutionEventArgs args) 
        { 
            Console.WriteLine(
this .GetType()  +   " : "   +   " ExecuteSuccess " ); 
        } 

        
#endregion  

        
#region  IMethodInject Members 

               
#endregion  
    }

将会转化(实际注入IL,这里反编译为了c#代码,更清晰)为:

 

 

image

 

从这里你就会清晰的明白这里实现静态注入了机制和原理了。我们需要做的目的就是从IL出发改变原来代码逻辑,注入我们的截取代码。使用Mono.Cecil具体代码在程序包MethodILInjectTask中。

MatchedMethodInterceptBase是应用于class上匹配该class多个methodattribute基类。rule为匹配规则。

[TestAOP2Attribute(Rule  =   " TestMethod1* " )]

public   class  Class1 


   

这里需要对于继承制该基类的标示class的所有满足rule的方法进行注入。

PropertyInterceptBase:属性注入,Action属性标识get,set方法。

[TestAOPPropertyGetAttribute(Action  =  PropertyInterceptAction.Get)] 
       
public   int  TestProperty 
       { 
           
get
           
set
       }

 

 

属性注入找出标示property,更具action选择get,set方法注入IL逻辑。

现在对于方法中获取attribute通过反射,性能存在一定问题。完全可以在class中注入属性,延时加载,Dictionary类级缓存来减少这方面损失,还暂时没考虑加入。

    不是很会写blog,所以有什么不明白的可留言,上一篇,由于时间写的没头没尾的,估计大家都看的很迷茫,迷茫该怎么写。关于IL注入Mono.Cecil可以参见编译时MSIL注入--实践Mono Cecil(1)和官方http://www.mono-project.com/Cecil。还有必须对MSIL具有一定了解(相同与Emit的IL注入)

   附带:源码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值