免费的网站关键词查询工具,wordpress重启,有哪些网站可以做海报设计知乎,书签制作方法图片简单又好看实现静态AOP#xff0c;就需要我们在预编译时期#xff0c;修改IL实现对代码逻辑的修改。Mono.Cecil就是一个很好的IL解析和注入框架#xff0c;参见编译时MSIL注入--实践Mono Cecil(1)。 我的思路为#xff1a;在编译时将加有继承制MethodInterceptBaseAttribute标签的原方… 实现静态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#代码更清晰为 从这里你就会清晰的明白这里实现静态注入了机制和原理了。我们需要做的目的就是从IL出发改变原来代码逻辑注入我们的截取代码。使用Mono.Cecil具体代码在程序包MethodILInjectTask中。 MatchedMethodInterceptBase是应用于class上匹配该class多个methodattribute基类。rule为匹配规则。 [TestAOP2Attribute(Rule TestMethod1*)] public class Class1 这里需要对于继承制该基类的标示class的所有满足rule的方法进行注入。 PropertyInterceptBase属性注入Action属性标识getset方法。 [ TestAOPPropertyGetAttribute(Action PropertyInterceptAction.Get)] public int TestProperty { get; set; } 属性注入找出标示property更具action选择getset方法注入IL逻辑。 现在对于方法中获取attribute通过反射性能存在一定问题。完全可以在class中注入属性延时加载Dictionary类级缓存来减少这方面损失还暂时没考虑加入。 不是很会写blog所以有什么不明白的可留言上一篇MSBuild MSILInect实现编译时AOP之预览由于时间写的没头没尾的估计大家都看的很迷茫迷茫该怎么写。关于IL注入Mono.Cecil可以参见编译时MSIL注入--实践Mono Cecil(1)和官方http://www.mono-project.com/Cecil。还有必须对MSIL具有一定了解相同与Emit的IL注入 附带××× 转载于:https://blog.51cto.com/whitewolfblog/835177