zoukankan      html  css  js  c++  java
  • AOP之PostSharp3MethodInterceptionAspect

       在上两篇我们介绍了OnExceptionAspectOnMethodBoundaryAspect ,在这节我们将继续了解MethodInterceptionAspect,他为我们提供了关于方法处理的AOP切入,不同于OnMethodBoundaryAspect,他不是边界,是方法体。有了我们可以在我们的方法切入aspect很多有用的信息,比如将同步方法变为异步,防止多次点击重复提交,winform,wpf的多线程调用UI(参见PostSharp - Thread Dispatching(GUI多线程)),长时间操作在超过用户接受时间弹出进度条等等有用的关于用户体验和业务逻辑功能,简化我们的编程开发。

    同样我们先来看看其MethodInterceptionAspect定义:

    image

    Invoke MethodInterceptionArgs参数:

    image

    我们一般使用Proceed是的方法进行处理。在这时我们可以加入线程池调用,使的其变为异步操作。

    同时MethodInterceptionAspect 还继承了MethodLevelAspect 的CompileTimeValidate编译是验证,CompileTimeInitialize编译时初始化,RuntimeInitialize运行时初始化,后边的初始化我们将在后面一节PostSharp范围(static和instance中讲到)。

    其定义很简单,在于我们的发挥:

    二:防止多次提交处理demo:

    我们这里只采用简单思路在方法进入禁止按钮可用,方法执行完成后恢复可用状态。我们将使监听winform事件处理方法,按钮来自EventHandle的第一个参数Sender。

    [Serializable] 
        public class UnMutipleTriggerAttribute : MethodInterceptionAspect 
        { 
           

    public override bool CompileTimeValidate(System.Reflection.MethodBase method) 
           { 
               var ps = method.GetParameters(); 
               if (ps != null && ps.Count() > 0 && ps[0].Name == "sender"
                   return true
               return false
           } 

            public override void OnInvoke(MethodInterceptionArgs args) 
            { 
                if (args.Arguments.Count > 0
                { 
                    var controls = args.Arguments[0as System.Windows.Forms.Control; 
                    if (controls != null && controls.Enabled) 
                    { 
                        controls.Enabled = false
                        args.Proceed(); ; 
                        controls.Enabled = true
                    } 
                } 

            } 
        }

    在这里我们是监听方法的处理事件函数根据vs自动生成规则,第一个参数是sender,事件源,这里利用了CompileTimeValidate在编译时决定是否注入aspect。

    注意这里只是一个简单的demo,只针对于同步操作,如要变为异步操作,这需要改为在异步操作后启用。

    测试在button点击方法加上attribute:

    [UnMutipleTriggerAttribute] 
            private void Save(object sender, EventArgs e) 
            { 
                System.Threading.Thread.Sleep(2000); 
            }

    效果:

    image

    这个例子很简单的就完成了。

    demo下载

    参考:

  • AOP之PostSharp初见-OnExceptionAspect
  • AOP之PostSharp2-OnMethodBoundaryAspect
  • AOP之PostSharp3-MethodInterceptionAspect
  • AOP之PostSharp4-实现类INotifyPropertyChanged植入
  • AOP之PostSharp5-LocationInterceptionAspect
  • AOP之PostSharp6-EventInterceptionAspect
  • http://www.cnblogs.com/whitewolf/category/312638.html  
查看全文
  • 相关阅读:
    iOS开发之即时通讯之Socket(AsyncSocket)
    iOS 拨打电话功能
    iOS text的长文本换行
    IOS Mac上查看iPhone上的SQlite数据库
    iOS Code Signing的Invalid处理(根证书无效)
    判断UITextField 输入为空 输入全为空格
    IOS Constraints自动布局适应不同尺寸
    IOS App打包发布流程(公司账号)
    IOS App发布问题:code signing is required for product type "Application" in SDK 'iOS 9.2'
    IOS UIAlertView 和 UIActionSheet的区别
  • 原文地址:https://www.cnblogs.com/whitewolf/p/PostSharp3.html
  • Copyright © 2011-2022 走看看