【发布时间】:2011-02-09 04:13:59
【问题描述】:
我有一些代码使用反射从对象中提取属性值。在某些情况下,属性可能会抛出异常,因为它们具有空引用等。
object result;
try
{
result = propertyInfo.GetValue(target, null);
}
catch (TargetInvocationException ex)
{
result = ex.InnerException.Message;
}
catch (Exception ex)
{
result = ex.Message;
}
最终代码可以正常工作,但是当我在调试器下运行时:
当属性引发异常时,IDE 会像未捕获异常一样进入调试器。如果我只是点击运行,程序就会通过,异常作为 TargetInvocationException 出现,而真正的异常在 InnerException 属性中。
我怎样才能阻止这种情况发生?
【问题讨论】:
标签: c# exception reflection propertyinfo targetinvocationexception