这个真的在烤我的面条......
我有一些代码。当我运行它时,Visual Studio 提示出现未处理的异常。果然, 有一个异常(exception)。但是,它明目张胆地位于 try/catch block 中,所以...“未处理”是怎么回事??
我四处搜索,我能找到的唯一相关结果与 LINQ 有关。但我没有使用任何 LINQ,所以不可能。
相关代码如下所示:
try
{
method.Invoke(target, new object[0]);
}
catch (MyException e)
{
Console.WriteLine(e);
}
Invoke() 命中的方法每次都不同。其中一些方法确实会抛出异常——这很好。但这就是为什么我把它放在 try-block 中!我不明白为什么 VS 提示没有处理异常。
(是的,我检查过抛出的异常是一个 MyException 对象。)
在这一点上,我唯一的猜测是它在某种程度上与使用反射有关。不过我真的需要解决这个问题……有什么建议吗?
请您参考如下方法:
您应该阅读 MethodInfo.Invoke 的文档.
Exceptions
TargetInvocationException - The invoked method or constructor throws an exception.
Remarks
If the invoked method throws an exception, the
Exception.GetBaseExceptionmethod returns the exception.
您的异常被包装在 TargetInvocationException 中.
