C#反射问题,Invoke调用的方法异常处理
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Test1 test = new Test1();
foreach (var item in test.GetType().GetMethods())
{
try
{
item.Invoke(test, null);
}
catch (Exception)
{
MessageBox.Show("捕获异常");
}
}
}
public class Test1
{
public void method1()
{
throw new Exception("有异常");
}
public void method2()
{
throw new NotImplementedException();
}
}
}
new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Application.Run(new Form1());
}
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
}