zoukankan      html  css  js  c++  java
  • 解决异常:无效程序的通用语言检测的实施

    我碰到这个问题比較奇怪,在开发企业即时通信系统  OrayTalk  组织结构功能时碰到的我写的一个方法(基于.NET 2.0)在win7、win2003下执行没有问题,在winxp下执行就抛异常:“公共语言执行时检測到无效的程序”。相应英文为:common language runtime detected an invalid program.

      抛异常的方法代码摘抄例如以下:

    复制代码
        private Control control = ...;
        public void ActionOnUI<T1>(bool showMessageBoxOnException, bool beginInvoke,  CbGeneric<T1> method, params object[] args)
        {
            if (this.control.InvokeRequired)
            {
                if (beginInvoke)
                {
                    this.control.BeginInvoke(new CbGeneric<bool, bool, CbGeneric<T1>, object[]>(this.ActionOnUI), showMessageBoxOnException, beginInvoke, method, args);
                }
                else
                {
                    this.control.Invoke(new CbGeneric<bool, bool, CbGeneric<T1>, object[]>(this.ActionOnUI), showMessageBoxOnException, beginInvoke, method, args);
                }
            }
            else
            {
                try
                {
                    method((T1)args[0]);
                }
                catch (Exception ee)
                {                if (showMessageBoxOnException)
                    {
                        MessageBox.Show(ee.Message);
                    }
                }
            }
        }
    复制代码

        方法的目的是对UI调用转发做一个封装,让使用者更方便的将调用转发到UI线程。

        可是。这种方法在执行时,异常在xp下发生了:

         Common Language Runtime detected an invalid program.

              at ESBasic.Helpers.UiSafeInvoker.ActionOnUI[T1](Boolean showMessageBoxOnException, Boolean beginInvoke, CbGeneric`1 method, Object[] args)

         我在网上搜了一些相关问题的解答,比較靠谱的一点是这样说的:

        “这样的错误很少见,是一个编译器错误,通常产生在将C#等托管语言生成为MSIL时候出的错,没有什么好的解决的方法。如今可行的方法好像就是改动如今的程序结构。这样依据新的结构生成新的MSIL时不会出错就基本能够避免这个问题。”

        依据这个提示,我对方法的代码进行了各种改动尝试,最后最终得到了一种在xp下也不抛异常的结构,粘贴例如以下:

    复制代码
        private Control control = ...;
        public void ActionOnUI<T1>(bool showMessageBoxOnException, bool beginInvoke, CbGeneric<T1> method, T1 args)
        {
            if (this.control.InvokeRequired)
            {
                if (beginInvoke)
                {
                    this.control.BeginInvoke(new CbGeneric<bool, CbGeneric<T1>, T1>(this.Do_ActionOnUI<T1>), showMessageBoxOnException, method, args);
                    return;
                }
                this.control.Invoke(new CbGeneric<bool, CbGeneric<T1>, T1>(this.Do_ActionOnUI<T1>), showMessageBoxOnException, method, args);
                return;
            }
    
            this.Do_ActionOnUI<T1>(showMessageBoxOnException, method, args);
        }
    
        private void Do_ActionOnUI<T1>(bool showMessageBoxOnException, CbGeneric<T1> method, T1 args)
        {
            try
            {
                method(args);
            }
            catch (Exception ee)
            {            if (showMessageBoxOnException)
                {
                    MessageBox.Show(ee.Message);
                }
            }
        } 
    复制代码

      总结起来,改变的几点例如以下:

    (1)将真正执行的部分重构为一个方法Do_ActionOnUI。然后。转发调用Invoke都指向这种方法。

    (2)Invoke转发调用时。为指向的方法加上泛型參数,避免编译器自己主动去匹配。

    (3)将弱类型的參数object[]改动为强类型的參数T1。

      好吧,今天的问题是最终解决。好折腾了不少啊~~

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    javascript cookie
    mark几个比较好的配色网站
    Javascrip 淡入淡出思路
    实验报告:统计字符串中子字符串出现的次数
    Javascript计算器
    《node入门》学习
    配置ionic(低版本)
    eclipse环境配置
    关于文档加载的方法
    javascript基础-《web前端最佳实践》
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4853664.html
Copyright © 2011-2022 走看看