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

时间:2014-03-26 15:00:53

标签: c# windows reflection .net-assembly uncaughtexceptionhandler

                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;
        }
    }

请告诉我如何捕获表单上按钮点击事件发生的异常 这是使用方法CreateFormInstance()创建的。

我的按钮点击代码是

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

我想在创建此表单的父表单中使用异常。

1 个答案:

答案 0 :(得分:0)

您可以将事件处理程序添加到' Application.ThreadException'这将允许您运行代码并导致您的应用程序在您的UI(主)线程上抛出未捕获的异常时不会退出。另外,您还可以处理' AppDomain.CurrentDomain.UnhandledException'这将允许您在应用程序中的任何线程上抛出异常时运行代码,但不会阻止您的应用程序退出