AOP之PostSharp初见-OnExceptionAspect

简介:

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

一:OnExceptionAspect

起定义如下:

image

先写Aspect Attribute:

ExpandedBlockStart.gif
复制代码
[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)。

看看我们的测试代码:

ExpandedBlockStart.gif
复制代码
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--破狼。http://www.cnblogs.com/whitewolf/archive/2011/12/04/PostSharp1.html


相关文章
|
11天前
|
Java Spring
切面编程的锋芒:Spring切入点的玩法与技巧
切面编程的锋芒:Spring切入点的玩法与技巧
15 0
切面编程的锋芒:Spring切入点的玩法与技巧
|
8月前
|
XML 监控 Java
AOP开发
本文介绍AOP切面编程思想开发
|
10月前
|
Java Spring 容器
|
11月前
|
Java Spring 容器
面试被问了几百遍的 IoC 和 AOP,还在傻傻搞不清楚?
首先声明:IoC & AOP 不是 Spring 提出来的,它们在 Spring 之前其实已经存在了,只不过当时更加偏向于理论。Spring 在技术层次将这两个思想进行了很好的实现。
|
监控 安全 Java
AOP的点点滴滴
首先AOP是一种思想,我们不能将它局限于某种语言
80 0
AOP的点点滴滴
|
XML 监控 Java
【SSM直击大厂】第十章:Spring AOP面向切面编程
📋📋 精彩摘要:Spring AOP面向切面编程是Java OOP面向对象编程的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍 生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
107 0
|
XML Java 数据库
【AOP】如期而至,你准备好了吗!
本文主要介绍 Spring 中 AOP的XML配置开发
83 0
|
设计模式 缓存 安全
Spring框架学习(五) 面向切面编程 AOP
Spring框架学习(五) 面向切面编程 AOP
126 0
Spring框架学习(五) 面向切面编程 AOP
|
监控
【ABP杂烩】面向切面编程(AOP)知识总结
原文:【ABP杂烩】面向切面编程(AOP)知识总结 目录 1.存在问题 2.AOP的概念 3.AOP应用范围 3.AOP实现方式 4.应用举例 5.结束语 本文在学习【老张的哲学】系列文章AOP相关章节后,自己归纳总结的笔记。
1149 0

热门文章

最新文章