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

查看:1179
本文介绍了Reflection 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.

我收到以下消息:


用户未处理异常。

Exception was unhandled by the user.



为什么 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答案中提到的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 之外,并调用它。使用这种技术,抛出的异常将不被包装。此外,这种方法似乎可以很好地与调试器一起播放;我没有得到任何未捕获的例外弹出窗口。

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.

/ p>

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

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

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