我在WinForms应用程序中工作,并使用BindingList数据源。我需要用PropertyDescriptor.检查对象是否有效。因为PropertyDescriptor.GetValue(object obj)将适用于有效的对象。但有时我有"TargetInvocationException".因此,在得到值之前,我想检查该对象是否有效。
[https://i.stack.imgur.com/VsdeW.png]
下面是堆栈跟踪:
System.Reflection.TargetException: Object does not match target type.
at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.SecurityUtils.MethodInfoInvoke(MethodInfo method, Object target, Object[] args)
at System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component)
--- End of inner exception stack trace ---
at System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component)发布于 2017-03-07 13:29:41
在您已经需要执行调用的情况下,只要尝试调用并在失败时执行一些不同的操作,就会简单得多,成本也会低得多。
try
{
PropertyDescriptor.GetValue(...);
}
catch (TargetException ex)
{
// do the thing you would do if the object wasn't valid.
}https://stackoverflow.com/questions/42649406
复制相似问题