反射 MethodInfo.Invoke() 从方法内部捕获异常 [英] Reflection MethodInfo.Invoke() catch exceptions from inside the method

查看:20
本文介绍了反射 MethodInfo.Invoke() 从方法内部捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调用了 MethodInfo.Invoke() 以通过反射执行函数.该调用包含在 try/catch 块中,但它仍然无法捕获我正在调用的函数抛出的异常.

I have a call to MethodInfo.Invoke() to execute a function through reflection. The call is wrapped in a try/catch block but it still won't catch the exception thrown by the function I'm invoking.

我收到以下消息:

用户未处理异常.


为什么 MethodInfo.Invoke() 会阻止在 Invoke() 之外捕获异常?
我该如何绕过它?


Why does MethodInfo.Invoke() prevent the Exception to be caught outside of the Invoke()?
How do I bypass it?

推荐答案

据我了解您的问题,该问题纯粹是 IDE 问题;您不喜欢 VS 将调用 MethodInfo 引发的异常视为未捕获,而显然不是.您可以在此处阅读有关如何解决此问题的信息:为什么是 TargetInvocationException是否被 IDE 视为未捕获? 这似乎是一个错误/设计使然;但无论如何,该答案中列出了体面的解决方法.

As I understand your issue, the problem is purely an IDE one; you don't like VS treating the exception thrown by the invocation of the MethodInfo as uncaught, when it clearly isn't. You can read about how to resolve this problem here: Why is TargetInvocationException treated as uncaught by the IDE? It appears to be a bug / by design; but one way or another, decent workarounds are listed in that answer.

在我看来,您有几个选择:

As I see it, you have a couple of options:

  1. 您可以使用MethodInfo.Invoke,捕获TargetInvocationException 并检查其InnerException 属性.您必须解决该答案中提到的 IDE 问题.

  1. You can use MethodInfo.Invoke, catch the TargetInvocationException and inspect its InnerException property. You will have to workaround the IDE issues as mentioned in that answer.

您可以从 MethodInfo 创建一个合适的 Delegate 并调用它.使用这种技术,抛出的异常将不会被包装.此外,这种方法确实似乎可以很好地与调试器配合使用;我没有收到任何未捕获的异常"弹出窗口.

You can create an appropriate Delegate out of the MethodInfo and invoke that instead. With this technique, the thrown exception will not be wrapped. Additionally, this approach does seem to play nicely with the debugger; I don't get any "Uncaught exception" pop-ups.

这是一个突出两种方法的示例:

Here's an example that highlights both approaches:

class Program
{
    static void Main()
    {
        DelegateApproach();
        MethodInfoApproach();
    }

    static void DelegateApproach()
    {
        try
        {
            Action action = (Action)Delegate.CreateDelegate
                                   (typeof(Action), GetMethodInfo());
            action();
        }
        catch (NotImplementedException nie)
        {

        }
     }

    static void MethodInfoApproach()
    {
        try
        {
            GetMethodInfo().Invoke(null, new object[0]);
        }
        catch (TargetInvocationException tie)
        {
            if (tie.InnerException is NotImplementedException)
            {


            }
        }
    }

    static MethodInfo GetMethodInfo()
    {
        return typeof(Program)
                .GetMethod("TestMethod", BindingFlags.NonPublic | BindingFlags.Static);
    }    

    static void TestMethod()
    {
        throw new NotImplementedException();
    }
}

这篇关于反射 MethodInfo.Invoke() 从方法内部捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆