使用 InnerException 就可以显示出反射调用里头的错误信息。

 

 
  
  1. void fn(int n)  
  2. {  
  3.     if (n >= 10)  
  4.         return;  
  5.     throw new ArgumentException("参数必须大于10");  
  6. }  

 

 
  
  1. Type type = typeof(Program);  
  2. object instance = Activator.CreateInstance(type);  
  3. MethodInfo mi = type.GetMethod("fn", BindingFlags.NonPublic | BindingFlags.Instance);  
  4. try 
  5. {  
  6.     mi.Invoke(instance, new object[] { null });  
  7. }  
  8. catch(TargetInvocationException targetEx)  
  9. {  
  10.     if (targetEx.InnerException != null)  
  11.     {  
  12.         throw targetEx.InnerException;  
  13.     }  
  14. }