在使用反射机制动态调用函数(通过MethodInfo.Invoke)时,发现无法捕获被调用函数抛出的异常。被调用函数抛出的异常被TargetInvocationException包装了。
可以通过以下方法处理被调用函数抛出的自定义异常:
try
{
method.Invoke(obj, parameters);
}
catch (System.Reflection.TargetInvocationException ex)
{
if (ex.InnerException != null)
{
Exception exTmp = ex.InnerException;
if(exTmp is xxxException)
{
}
}
}
记录下,也许会帮到你