![](/img/trans.png)
[英]System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation
[英]Can a TargetInvocationException thrown from reflection have a null InnerException
当浏览器源代码System.ComponentModel.DataAnnotations.CustomValidationAttribute
在这里,我看到了下面的代码(缩短):
try
{
methodInfo.Invoke(null, methodParams);
}
catch (TargetInvocationException ex)
{
if (ex.InnerException != null)
{
throw ex.InnerException
}
throw;
}
这里,代码检查ex.InnerException
是否为空。 我不认为如果TargetInvocationException
是从反射调用中抛出的,那么它永远不会有null
InnerException
。
这可能吗? 如果是这样,请提供一个场景,其中InnerException
可以为 null。
MSDN指出
创建时,TargetInvocationException 被传递一个对通过反射调用的方法抛出的异常的引用。 InnerException 属性保存基础异常。
所以理论上只使用框架反射方法它永远不应该为空......理论上:P
当然,如果您从正在调用的方法中显式地抛出它,它可以(并且将会!)为 null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.