.NET中的ExceptionHandler

时间:2018-06-25 08:25:39

标签: c# .net try-catch cil mono.cecil

大家好!

我使用Mono.Cecil复制一个方法,并且遇到以下问题:

我的复制功能很简单(例如)

public static bool Ping()
{
   MessageBox.Show("Ping");
   try
   {
       MessageBox.Show("status");
   }
   catch (Exception exception)
   {
       MessageBox.Show("Exception ");
       return false;
   }
   return true;
}

这没问题。但是如果我这样做:

MessageBox.Show("Exception " + exception.Message);

我很麻烦:当执行到达函数调用时,它会生成“未处理的异常”,clr完成工作。我什至看不到MessageBox.Show("Ping")

我复制try / catch块,这样:

    foreach (ExceptionHandler exh in SourceMethod.Body.ExceptionHandlers)
    {
        var ex = new ExceptionHandler(exh.HandlerType)
        {
            TryStart = exh.TryStart,
            TryEnd = exh.TryEnd,
            HandlerStart = exh.TryEnd,
            HandlerEnd = exh.HandlerEnd,
            CatchType = Assembly.MainModule.Import(typeof(Exception)), 
            HandlerType = exh.HandlerType,
            FilterStart = exh.FilterStart
        };
        target.Body.ExceptionHandlers.Add(ex);
   }

我认为我的问题在这里:

        CatchType = Assembly.MainModule.Import(typeof(Exception)), 

但是我不知道如何正确地做到这一点

我尝试过:

CatchType = exh.CatchType 

但不幸的是。

如何解决这种情况?有什么想法吗?

1 个答案:

答案 0 :(得分:0)

很可能您正在生成无效的 IL,并且运行时正在检测到它,甚至没有开始运行您的程序(因此您看不到 ping)。

您可以通过运行来检查(至少在 Windows 上):

<块引用>

验证组装路径

话虽如此,您可以在此处查看完整示例:certificate chain

相关问题