【问题标题】:Uncatcheable exception from MethodInfo.InvokeMethodInfo.Invoke 中无法捕获的异常
【发布时间】:2010-08-30 08:54:54
【问题描述】:

我有这个调用 MethodInfo 的代码:

try
{
     registrator.Method.Invoke(instance, parameters);
}
catch{
    registrator.FailureType = RegistratorFailureType.ExceptionInRegistrator;
    //registrator.Exception = e;
}

注册器只是一个 MethodInfo 包装器,Method 属性是 MethodInfo 对象本身。 parameters 和 object[] 和 instance 是 Method 的声明类型的正确实例(使用 Activator.Create 创建)。

方法看起来像这样(我正在测试异常捕获):

class Test : Plugin, ITest
{
    public void Register(IWindow window)
    {
        throw new Exception("Hooah");
    }
}

问题是:异常永远不会被捕获,并且 Visual Studio 的未捕获异常气泡会弹出。

这是在带有 .NET 4.0 的 VS 2010 中

【问题讨论】:

  • 您可以,也许,喜欢,也许,告诉我们例外是什么?
  • 你能写出一个小而完整的例子程序来展示这个问题吗?
  • 哪种类型的注册器是实例?可能问题出在 catch 子句中(FailureType 或 registrator 为 null)
  • @Timwi:你的意思是未捕获的异常?这正是 Register 方法中抛出的内容:带有消息“Hooah”的异常类型对象

标签: c# .net reflection


【解决方案1】:

问题不在于您的代码。
在“调试/异常”菜单中,删除所有检查。
它应该可以工作。

【讨论】:

  • 哦,谢谢,这似乎解决了我的问题。只是一个额外的问题:此设置是保存在项目文件中还是保存在用户 VS 配置中(我的意思是,如果其他人在他的 PC 上打开我的项目,是否也会在那里禁用异常)?
  • 啊谢谢...这就是我害怕的,我想没有办法绕过这个..?
【解决方案2】:

你试过了吗?

在 Program.cs 中

Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

然后对 Run 方法进行 try/catch:

try
{
    Application.Run(new Form1());
}
    catch (Exception ex)
{
}

【讨论】:

  • 该应用程序目前只有控制台。该代码稍后将在 WPF 应用程序中使用。
【解决方案3】:

问题不在于您显示的代码。

我试过了:

void Main()
{
    Test instance = new Test();
    object[] parameters = new object[] { null };

    MethodInfo method = typeof(Test).GetMethod("Register");

    try
    {
        method.Invoke(instance, parameters);
    }
    catch
    {
        Console.Out.WriteLine("Exception");
    }
}

interface ITest { }
interface IWindow { }
class Plugin { }

class Test : Plugin, ITest
{
    public void Register(IWindow window)
    {
        throw new Exception("Hooah");
    }
}

它按预期打印“异常”。您需要向我们展示更多代码。

如果我这样修改代码:

catch(Exception ex)
{
    Console.Out.WriteLine(ex.GetType().Name + ": " + ex.Message);
}

然后我得到一个 TargetInvocationException,其中 InnerException 属性是您抛出的异常。

【讨论】:

    【解决方案4】:

    我认为问题在于您期待特定的异常类型,可能是IOException 或其他什么,但实际上MethodInfo.Invoke() 会抛出TargetInvocationException

    try
    {
         registrator.Method.Invoke(instance, parameters);
    }
    catch (TargetInvocationException tie)
    {
        // replace IOException with the exception type you are expecting
        if (tie.InnerException is IOException)
        {
            registrator.FailureType = RegistratorFailureType.ExceptionInRegistrator;
            registrator.Exception = tie.InnerException;
        }
        else
        {
            // decide what you want to do with all other exceptions — maybe rethrow?
            throw;
            // or maybe unwrap and then throw?
            throw tie.InnerException;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-06
      • 1970-01-01
      • 2010-10-29
      • 1970-01-01
      • 2011-04-19
      • 2021-10-09
      • 2021-09-17
      • 2021-10-03
      相关资源
      最近更新 更多