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

查看:2874
本文介绍了从方法内部反射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() prevent的例外之外抓住了的invoke() <? BR>
我如何绕过它?


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

推荐答案

编辑:据我了解您的问题,这个问题纯粹是一个IDE之一;你不喜欢VS治疗由的MethodInfo 的调用如未捕获,抛出的异常时,它显然不是。您可以了解如何在这里解决此问题:<一href=\"http://stackoverflow.com/questions/2658908/why-is-targetinvocationexception-treated-as-uncaught-by-the-ide\">Why是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 并调用了吧。利用这种技术,将抛出的异常不会被包裹。此外,这种方法的确实的似乎与调试器发挥很好;我没有得到任何​​未捕获异常的弹出窗口。

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天全站免登陆