TargetInvocationException: Exception has been thrown by the target of an invocation.
反射某个方法的时候,如果这个方法内部逻辑写出Bug了,try这个Invoke的时候只会抛出这个异常。具体的异常,需要把InnerException也输出出来才知道。
try
{
methodInfo.Invoke(instance, new object[] { });
}
catch(TargetInvocationException e)
{
if(e.InnerException != null)
{
Console.WriteLine(e.InnerException.Message);
Console.WriteLine(e.InnerException.StackTrace);
}
else
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}