Resolve exception: The common language runtime detects an invalid program

Source: Internet
Author: User

I was surprised to find that when developing Oraytalk's organizational features, one of the methods (based on. NET 2.0) runs without problems in Win7, Win2003, and runs under WinXP, throwing Exceptions: "The common language runtime detects invalid programs", The corresponding English is: Common language runtime detected an invalid program.

The exception-throwing method code excerpt is as follows:

PrivateControl control = ...; Public voidActiononui<t1> (BOOLShowmessageboxonexception,BOOLBeginInvoke,Cbgeneric<T1> method,params Object[] args) {        if( This. Control. invokerequired) {if(BeginInvoke) { This. Control. BeginInvoke (Newcbgeneric<BOOL,BOOL,Cbgeneric<t1>,Object[]> ( This.            Actiononui), Showmessageboxonexception, BeginInvoke, method, args); }            Else            {                 This. Control. Invoke (Newcbgeneric<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); }            }        }    }

 The purpose of the method is to make the UI call forwarding a wrapper that makes it easier for the user to forward the call to the UI thread.

However, when this method executes, the exception occurs under XP:

Common Language Runtime detected an invalid program.

At Esbasic.helpers.uisafeinvoker.actiononui[t1] (Boolean showmessageboxonexception, Boolean BeginInvoke, CbGeneric ' 1 method, object[] args)

I searched the internet for some answers to the relevant questions, the more reliable point is that:

"This kind of error is very rare, is a compiler error, usually generated in C # and other managed language generation MSIL when the error, there is no good solution, now the feasible method seems to be to modify the current program structure , This problem can be avoided by generating new MSIL based on the new structure without error. ”

According to this hint, I have made various attempts to modify the code of the method and finally got a structure that does not throw an exception under XP, Paste as follows:

    Private ControlControl = ...; Public voidActiononui<t1> (BOOLShowmessageboxonexception,BOOLBeginInvoke,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 (Newcbgeneric<BOOL,Cbgeneric<t1>, t1> ( This. Do_actiononui<t1>), Showmessageboxonexception, method, args); return; }         This. Do_actiononui<t1>(Showmessageboxonexception, method, args); }    Private voidDo_actiononui<t1> (BOOLShowmessageboxonexception,Cbgeneric<T1>method, T1 args) {        Try{method (args); }        Catch(Exception ee) {if(showmessageboxonexception) { MessageBox. Show (EE.            Message); }        }    } 

  Summing up, the changes are as follows:

(1) Refactor the part that is actually executed into a method Do_actiononui, and then forward invoke invoke all points to this method.

(2) When invoke forwards the call, add the generic parameter to the method that points to, and avoid the compiler to automatically match.

(3) Modify the weakly typed parameter object[] to a strongly typed parameter T1.

Well, the problem is finally solved, a good toss AH ~ ~

Resolve exception: The common language runtime detects an invalid program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.