我正在通过反射调用一个可能导致异常的方法.如何在没有包装反射的情况下将异常传递给调用方?
我正在重新引发InnerException,但这会 destruct 堆栈跟踪.
示例代码:
public void test1()
{
// Throw an exception for testing purposes
throw new ArgumentException("test1");
}
void test2()
{
try
{
MethodInfo mi = typeof(Program).GetMethod("test1");
mi.Invoke(this, null);
}
catch (TargetInvocationException tiex)
{
// Throw the new exception
throw tiex.InnerException;
}
}