zoukankan      html  css  js  c++  java
  • 无需Try catch 的UI事件封装类

    在UI处理中,经常需要进行异常处理,以便在错误发生时能够进行一些自定义的操作,比如,弹出消息框给用户,进行重试操作,记录日志等,如果能够让用户写代码时不用写try...catch,而只是关注业务逻辑的处理,那么开发的效率将得到显著的提升。基于这个目的,我下面对EventHandler进行了封装,在用户进行事件调用时,自动给加上了一个try...catch块,用户捕捉客户端异常,并弹出提示框进行提示。

     public class EventHandlerWrapper   

      {        

            #region Property

            public object Target { get; private set; }

            public MethodInfo Method { get; private set; }

            public EventHandler Hander { get; private set; }

            #endregion

            #region Cnstructor

            public EventHandlerWrapper(EventHandler eventHandler)        

           {            

                  if (null == eventHandler)            

                  {                

                      throw new ArgumentNullException("eventHandler");  

                   }

                  this.Target = eventHandler.Target;          

                  this.Method = eventHandler.Method;          

                  this.Hander += Invoke;      

            }

            #endregion

            #region Public Method

            public static implicit operator EventHandler(EventHandlerWrapper eventHandlerWrapper)       

            {           

                     return eventHandlerWrapper.Hander;       

            }

            #endregion

            #region Private Method

            private void Invoke(object sender, EventArgs args)        

          {            

                   try     

                  {               

                        this.Method.Invoke(this.Target, new object[] { sender, args });      

                  }            

                 catch (TargetInvocationException ex)      

                 {            

                        object[] attbute = Method.GetCustomAttributes(typeof(MessageAttribute), true);        

                        MessageBox.Show((attbute[0] as MessageAttribute).Message,"提示", MessageBoxButtons.OK, MessageBoxIcon.Information);// + Environment.NewLine + "For detailed information, please view event log", string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);         

                }      

          }

            #endregion   

      }

    上面通过反射调用事件的委托方法,并放置在try...catch块中,在catch中,通过查找委托方法中的MessageAttribute,并将attribute中的message通过messagebox的方式弹出来显示给用户。

    MessageAttribute的定义如下:

        public class MessageAttribute:Attribute    

    {        

            public string Message { get; private set; }

            public MessageAttribute(string message)       

           {            

                  this.Message = message;     

            }    

    }

    其中在MessageAttribute中可以定义更加复杂的逻辑,比如是否记录日志,异常处理策略,与EnterpriseLibrary集成逻辑等,失败重试逻辑等等,更加复杂的处理逻辑。

    其应用示例如下:

    委托注册:

          public Form1()        

         {            

               InitializeComponent();

                this.button1.Click += new EventHandlerWrapper(this.button1_Click);        

        }

    委托方法:

            [Message("启动检验码工具失败")]        

            private void button1_Click(object sender, EventArgs e)        

          {            

                  throw new ApplicationException("error");     

          }

  • 相关阅读:
    Android 获取系统相册中的所有图片
    80 端口被占用 pid=4
    Android GridView 通过seletor 设置状态和默认状态
    Could not create SSL connection through proxy serve-svn
    Android 自定义 attr
    android 使用Tabhost 发生could not create tab content because could not find view with id 错误
    CodeSimth
    Android Ormlite 学习笔记2 -- 主外键关系
    Android Ormlite 学习笔记1 -- 基础
    Nhibernate的Session管理
  • 原文地址:https://www.cnblogs.com/lenmom/p/3637992.html
Copyright © 2011-2022 走看看