捕获由Assembly.CreateInstance()方法Reflection创建的表单上的异常
本文关键字:创建 Reflection 表单 异常 方法 Assembly CreateInstance | 更新日期: 2023-09-27 17:53:30
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");
}
我想要创建此表单的父表单中的异常。
您可以将事件处理程序添加到"Application.ThreadException",这将允许您运行代码,并导致应用程序在UI(主(线程上引发未捕获的异常时不退出。或者,您也可以处理"AppDomain.CurrentDomain.UnhandledException",这将允许您在应用程序中的任何线程上引发异常时运行代码,但不会阻止应用程序退出