AOP之PostSharp6-EventInterceptionAspect(事件异步调用)

    在上几章我们讨论了方法属性字段的aspect,现在我们再来看看事件机制的aspect。和字段,属性location一样,在c#中字段也可以转化为方法名为add,remove的方法处理,所以对于事件的aspect,同样类似于我们的方法。我们先看看EventInterceptionAspect的定义:

AOP之PostSharp6-EventInterceptionAspect(事件异步调用)

aspect类包含我们对于事件aspect所必要的注册,取消,调用的注入。

其参数定义如下:

AOP之PostSharp6-EventInterceptionAspect(事件异步调用)

为我们提供了,ProceedAddHandler,ProceedInvokeHandler,ProceedRemoveHandler的事件处理代理。同样包含来自AdviceArgs的Instance对象。

  对于事件aspect的例子真的不好想,在这里我们只是简单的做个事件变为异步调用的代码作为demo:

AOP之PostSharp6-EventInterceptionAspect(事件异步调用)
[Serializable]
    public class AsynEventAspectAttribute : PostSharp.Aspects.EventInterceptionAspect
    {

        public override void OnInvokeHandler(PostSharp.Aspects.EventInterceptionArgs args)
        {
            var th = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(new Action<object>((obj) =>
                {
                    System.Threading.Thread.Sleep(new Random().Next(1000));
                    try
                    {
                        args.ProceedInvokeHandler();
                    }
                    catch (Exception ex)
                    {

                        args.ProceedRemoveHandler();
                    }
                })));
            th.Start();
        }
    }
AOP之PostSharp6-EventInterceptionAspect(事件异步调用)

 测试代码:

AOP之PostSharp6-EventInterceptionAspect(事件异步调用)
namespace PostSharpDemo 

    public class TestAsyncAspect 
    { 
        [AsynEventAspectAttribute] 
        public event EventHandler SomeEvent = null

        public void OnSomeEvent() 
        { 
            if (SomeEvent != null
            { 

                SomeEvent(this, EventArgs.Empty); 
            } 
        } 
    } 
}
AOP之PostSharp6-EventInterceptionAspect(事件异步调用)
class Program 
    { 
        static void Main(string[] args) 
        {

TestAsyncAspect pro = new TestAsyncAspect(); 
          for (int i = 0; i < 10; i++) 
          { 
              pro.SomeEvent += new EventHandler(pro_SomeEvent); 
          } 
          pro.OnSomeEvent(); 
    //      pro.SomeEvent -= new EventHandler(pro_SomeEvent); 
          Console.WriteLine("主线程完了!"); 
          Console.Read(); 
      } 

      static void pro_SomeEvent(object sender, EventArgs e) 
      { 
          Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId); 
      }    

}  
AOP之PostSharp6-EventInterceptionAspect(事件异步调用)
AOP之PostSharp6-EventInterceptionAspect(事件异步调用)

 效果图如下:

AOP之PostSharp6-EventInterceptionAspect(事件异步调用)

附件下载: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


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