简体   繁体   English

捕获由Assembly.CreateInstance()方法反射创建的表单上的异常

[英]catch exception on a form which is created by Assembly.CreateInstance() method Reflection

                Form myForm = clsUIMethods.CreateFormInstance(iObjectAppId, sObjName, sFormCaption, sKey, toolCategory, sAccessibleDefaultActionDescription);




    public Form CreateFormInstance(int objectAppID, string formName, string formCaption, string formTag, string accessibleName, string accessibleDefaultActionDescription)
    {
        try
        {

            Assembly myAssembly = null;

            VersionInfo clsAssemblyInfo = GetAssemblyInfo(objectAppID);
            if (clsAssemblyInfo == null)
            {
                throw new Exception("Cannot open " + formCaption + ".\nPlease Check Your Application Menu Rights.", new Exception("Could not Load Assembly for : " + formName + "."));
            }

            formName = clsAssemblyInfo.NameSpace + "." + formName;

            try
            {
                if (objectAppID == 0)
                    myAssembly = Assembly.GetEntryAssembly();
                else if (objectAppID > 5000) //AppID above 5000 are for supporting projects 
                    myAssembly = Assembly.LoadFile(string.Format("{0}\\{1}.dll", Application.StartupPath, clsAssemblyInfo.AssemblyName));
                else
                    myAssembly = Assembly.Load(clsAssemblyInfo.AssemblyName);
            }
            catch
            {
                throw new Exception("Application file not Found.\nPlease Contact " + AppInstance.Name + " Co-ordinator.", new Exception("Could not Load Assembly for : " + clsAssemblyInfo.ApplicationName + "."));
            }

            //if (!clsAssemblyInfo.CheckVersionAtLogin)
            //{
            //    if (GetAssemblyVersion(myAssembly) != clsAssemblyInfo.Version)
            //    {
            //        throw new Exception("Object cannot be opened.\nYou are Running an Application of Different Version.", new Exception("Version mismatch for the Application : " + clsAssemblyInfo.ApplicationName + "."));
            //    }
            //}

            int iOverrideVersionControl = 0;
            try
            {
                if (ConfigurationManager.AppSettings["OverrideVersionControl"] != null)
                    iOverrideVersionControl = ConfigurationManager.AppSettings["OverrideVersionControl"].ToInt32();
            }
            catch { }

            if (iOverrideVersionControl != 1)
            {
                if (clsAssemblyInfo.CurrentAssemblyVersion != clsAssemblyInfo.Version)
                {
                    if (!clsAssemblyInfo.AllowOldVersion)
                    {
                        throw new Exception("Screen cannot be opened.\nYou are Running an Application of Different Version.", new Exception("Version mismatch for the Application : " + clsAssemblyInfo.ApplicationName + "."));
                    }
                    else
                    {
                        if (DisplayMessages("The application version of the screen being opened is different than the release version.\nDo you wish to continue ?", MessageStyle.YesNo, "Version mismatch for the Application : " + clsAssemblyInfo.ApplicationName + ".") == MessageResult.No)
                            throw new Exception("OLDVERSION");
                    }
                }
            }

            //if (objectAppID == 0)
            //    myAssembly = Assembly.GetEntryAssembly();
            //else
            //{
            //    try
            //    {
            //        myAssembly = Assembly.Load(clsAssemblyInfo.AssemblyName);
            //    }
            //    catch
            //    {
            //        throw new Exception("File not Found.\nPlease Contact " + AppInstance.Name + " Co-ordinator.", new Exception("Could not Load Assembly for : " + clsAssemblyInfo.ApplicationName + "."));
            //    }

            //    if (!clsAssemblyInfo.CheckVersionAtLogin)
            //    {
            //        if (GetAssemblyVersion(myAssembly) != clsAssemblyInfo.Version)
            //        {
            //            throw new Exception("Object cannot be opened.\nYou are Running an Application of Different Version.", new Exception("Version mismatch for the Application : " + clsAssemblyInfo.ApplicationName + "."));
            //        }
            //    }

            //    //string str = myAssembly.ImageRuntimeVersion; 
            //    //FileInfo fileInfo = new FileInfo(Directory.GetCurrentDirectory() + @"\" + assemblyName);
            //    //if (fileInfo.Exists)
            //    //myAssembly = Assembly.LoadFile(fileInfo.FullName);
            //    //else
            //    //    throw new Exception("File Not Found.\nPlease Contact " + AppInstance.Name + " Co-ordinator.", new Exception("Could not Load Assembly : " + assemblyName + "."));
            //}

            //myAssembly.GetName().CultureInfo = glMod.GetCultureInfo();

            Form myForm = myAssembly.CreateInstance(formName, true) as Form;
            if (myForm != null)
            {
                MainForm.Instance.AskBeforeClosingForm = true;
                if (formCaption != string.Empty)
                    myForm.Text = formCaption;
                myForm.Tag = formTag;
                myForm.AccessibleName = accessibleName;
                myForm.AccessibleDefaultActionDescription = accessibleDefaultActionDescription;
                myForm.KeyPreview = true;
                return myForm;
            }
            else
            {
                return null;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Please tell me how to catch exception which happens on a button click event on the form which is created using the method CreateFormInstance() . 请告诉我如何捕获在使用方法CreateFormInstance()创建的表单上的按钮单击事件上发生的异常。

My button click code is : 我的按钮点击代码是

  private void button1_Click(object sender, EventArgs e)
        {
            throw new NullReferenceException("nullref test muh sa");
        }

I want the exception in the parent form which created this form. 我想要创建该表单的父表单中的异常。

You can add an event handler to 'Application.ThreadException' this will allow you to run code and causes your application to not exit whenever a uncaught Exception is thrown on your UI(main) thread. 您可以在'Application.ThreadException'中添加事件处理程序,这将允许您运行代码,并导致在UI(main)线程上引发未捕获的Exception时,应用程序不会退出。 Alternitivly you can also handle 'AppDomain.CurrentDomain.UnhandledException' this will allow you to run code whenever an exception is thrown on any thread in you application but doesn't prevent your application from exiting 另外,您还可以处理“ AppDomain.CurrentDomain.UnhandledException”,这将使您可以在应用程序中的任何线程上抛出异常时运行代码,但不会阻止应用程序退出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM