0

저는 WinForms 응용 프로그램에서 작업 중이며 BindingList 데이터 소스를 사용했습니다. 객체가 유효한지 여부는 PropertyDescriptor으로 확인해야합니다. PropertyDescriptor.GetValue(object obj)이 유효한 개체로 작동하기 때문입니다. 하지만 때로는 "TargetInvocationException"이 있습니다. 그래서 그 개체가 유효한지 또는 값을 얻기 전에 확인하고 싶습니다.PropertyDescriptor에서 유효한 개체를 확인하는 방법은 무엇입니까?

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) 
+1

'시도/캐치'가 너무 비쌉니까? [CheckConsistency'] (https://referencesource.microsoft.com/#mscorlib/system/reflection/methodinfo.cs,9d661e8f9f8783cd) 메소드에서 유형을 확인할 수 있어야합니다 (리플렉션을 사용하여 비공개에 액세스해야 할 수도 있음). 회원). – Sinatr

+0

코드를 보여줄 수 있습니까? – Usman

답변

1

경우 이미, 그냥 전화를 시도 훨씬 쉽고 저렴 될 것입니다 호출을 실행하기 위해 필요 할 것입니다 : 여기에 [https://i.stack.imgur.com/VsdeW.png]

는 스택 트레이스입니다 실패 할 경우 다른 것.

try 
{ 
    PropertyDescriptor.GetValue(...); 
} 
catch (TargetException ex) 
{ 
    // do the thing you would do if the object wasn't valid. 
} 
+0

안녕하세요 @ Caesay, 귀하의 제안에 감사드립니다. 사실 나는 값 (PropertyDescriptor.GetValue)을 얻기 전에 속성의 유효성을 검사하려고합니다. 걱정되는 점이 있으면 알려주세요. – Prithiv