jeudi 2 mars 2017

TargetInvocationException+InvalidCastException on setting property

I have a WPF application. Dialog is called for some user input

new ConfigDialog().ShowDialog();

In the dialog after user clicks "Save" every property is saved to an object:

PropertyInfo pi = myObj.GetType().GetProperty("SomeColorProperty");
pi.SetValue(myObj, userColor, null);

Neither of myObj, pi, userColor, myObj.SomeColorProperty is null. However after the last string, TargetInvocationException is thrown at new ConfigDialog().ShowDialog(). MSDN states that InnerException indicates the reason. In my case, it is an InvalidCastException. But userColor is System.Windows.Media.Color, and myObject.SomeColorProperty is also System.Windows.Media.Color. I tried hard-coding the value, like

pi.SetValue(myObj, Color.FromRgb(10, 20, 30), null);

, but it is the same. What are the possible reasons for this trouble?





Aucun commentaire:

Enregistrer un commentaire