• AOP之PostSharp初见OnExceptionAspect


         PostSharp 这个静态植入的aop框架我就不多说了,在以前的aop文件,我们也尝试用MSBuild+Mono.Cicel理解静态植入AOP的原理。最近公司准备购买Postsharp做一些AOP,减少开发是代码量,至于选择AOP相信也不用多说。我也在今天重新了解了些Postsharp最新版更新,这阵子的博客更新也少了,所以准备在MSBuild+Mono.Cicel的基础上再一些Postsharp系列。今天既然是初见,那么我们就从最简单的OnExceptionAspect开始。

    一:OnExceptionAspect

    起定义如下:

    image

    先写Aspect Attribute:

    View Code
    [Serializable] 
        public class ExceptionAspectDemoAttribute : OnExceptionAspect 
        { 

            public override void OnException(MethodExecutionArgs args) 
            { 
                var msg = string.Format("时间[{0:yyyy年MM月dd日 HH时mm分}]方法{1}发生异常: {2}\n{3}", DateTime.Now, args.Method.Name, args.Exception.Message, args.Exception.StackTrace); 
                Console.WriteLine(msg); 
                args.FlowBehavior = FlowBehavior.Continue; 
            } 
            public override Type GetExceptionType(System.Reflection.MethodBase targetMethod) 
            { 
                return typeof(NullReferenceException); 
            } 
        }

    注意Postsharp的Aspect都需要标记为可序列化的,因为在编译时会为我们二进制序列化为资源,减少在运行是的开销,这个将在后面专门讲。

    上面的code继承至OnExceptionAspect,并且override OnException和GetExceptionType,GetExceptionType为我们需要处理的特定异常。OnException为异常处理决策方法。我们的异常处理决策是当NullReferenceException时候我们会记录日志,并且方法指定继续(args.FlowBehavior = FlowBehavior.Continue)。

    看看我们的测试代码:

    View Code
    class Program 
       { 
           static void Main(string[] args) 
           { 
               Program.ExceptionAspectDemoAttribute1(); 
               Program.ExceptionAspectDemoAttribute2(); 
               Console.Read(); 
           } 
           [ExceptionAspectDemo] 
           public static void ExceptionAspectDemoAttribute1() 
           { 
               string s = null
               s.GetType(); 
           } 
           [ExceptionAspectDemo] 
           public static void ExceptionAspectDemoAttribute2() 
           { 
               throw new Exception("exception"); 
           } 
       }

     很显然我们的两个方法抛出了null异常和自定义异常,预期是NullReferenceException会被扑捉,而自定义异常会中断,运行效果如下:

    image 

    我们在来看看postsharp为我们做了什么,当然是反编译看看:

    image

    二:Postsharp的Multicasting

    1:Multicasting class:

      在这随便也说一下postsharp的Multicasting,多播这样翻译感觉有点死板呵呵,理解就行。利用这一点我们可以吧我们的aspect放在class,assembly等目标上匹配我们的多个目标。比如现在我们不想在我们的每个方法上加attribute,那我们可以选择在class上,如:

    image

    反编译,同样注入了我们每个方法:

    image

    2:Multicasting assembly:

    我们同样可以利用

    [assembly: PostSharpDemo.ExceptionAspectDemoAttribute()]

    标记在我们的程序集上。

    3:AttributeExclude:

    但是注意这样也标记了我们的aspect,某些时候可能会导致堆栈溢出 ,我们可以用AttributeExclude=true来排除。

    同时我们也可以设置应用目标:AttributeTargetMemberAttributes是一个枚举类型,定义如下:

    image

    比如我们需要过滤编译时候生成的目标(自动属性,action等等),

    [assembly: PostSharpDemo1.MethodTraceAspect(AttributeExclude = true, AttributePriority = 0, AttributeTargetMemberAttributes = MulticastAttributes.CompilerGenerated)]

     4:AttributePriority:

    还有AttributePriority,我们可以设置编译时优先级。如果我们对目标标记了多个aspect,这样postsharp就不确定注入先后顺序,这样不能确保正确性,在vs编译时候我们会看见警告:Their order of execution is undeterministic.

    image

    这是时候AttributePriority就派上用途了来决定我们植入的先后优先级。

    5:其他匹配

    同上AttributeTargetMemberAttributes 我们还可以利用AttributeTargetMembers,AttributeTargetTypes进行目标名称的匹配,支持模糊匹配。

    附件:Demo下载

    我的AOP资料:

  • AOP之PostSharp初见-OnExceptionAspect
  • AOP之PostSharp2-OnMethodBoundaryAspect
  • AOP之PostSharp3-MethodInterceptionAspect
  • AOP之PostSharp4-实现类INotifyPropertyChanged植入
  • AOP之PostSharp5-LocationInterceptionAspect
  • http://www.cnblogs.com/whitewolf/category/312638.html


作者:破  狼
出处:http://www.cnblogs.com/whitewolf/
本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。该文章也同时发布在我的独立博客中-个人独立博客博客园--破狼51CTO--破狼

  • 相关阅读:
    jeecg 笔记之 自定义显示按钮 (exp 属性)
    jeecg 笔记之 自定义word 模板导出(一)
    jeecg 默认为空的字段值是如何被填充的?
    算法题——立方体的体对角线穿过多少个正方体?
    借用Snippet插件美化博客中的代码
    用PS设计等高线效果的背景图片
    算法题——投篮比赛获胜概率问题
    计算机中的颜色XIV——快速变换颜色的V分量
    算法实践——Twitter算法面试题(积水问题)的线性时间解法
    UI设计实战篇——利用Bootstrap框架制作查询页面的界面
  • 原文地址:https://www.cnblogs.com/whitewolf/p/PostSharp1.html
  • Copyright © 2020-2023  润新知