私はWinFormsアプリケーションで作業しており、BindingListデータソースを使用しています。オブジェクトが有効かどうかは、PropertyDescriptorで確認する必要があります。 PropertyDescriptor.GetValue(object obj)
が有効なオブジェクトとして動作するためです。しかし時々私は "TargetInvocationException"を持っています。だから私はそのオブジェクトが有効かどうか、値を取得する前にチェックしたい。PropertyDescriptorで有効なオブジェクトをチェックする方法は?
[ここ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)
'try/catch'は高価すぎますか? [CheckConsistency'](https://referencesource.microsoft.com/#mscorlib/system/reflection/methodinfo.cs,9d661e8f9f8783cd)メソッドのようにタイプをチェックすることができます(リフレクションを使用してプライベートにアクセスする必要があるかもしれません)メンバー)。 – Sinatr
あなたのコードを表示できますか? – Usman