samedi 24 octobre 2020

C# WPF - Object must implement IConvertible. when using GetProperties()[i]SetValue

I'm a beginner so expect overcomplicated shit code and dumb questions.

I have a static class called Options. I have a list of objects, called loadedVariables that have previous values of the properties of this class. I'm trying to set this class' properties to their previous values (the values in loadedVariables):

1.  for (int i = 0; i < loadedVariables.Count; i++)
2.  {
3.      var property = typeof(Options).GetProperties()[i];
4.      object variable = loadedVariables[i];
6.      property.SetValue(typeof(Options), Convert.ChangeType(variable, property.PropertyType), null);
7.  }

but line 6 throws a System.InvalidCastException 'Object must implement IConvertable'

I tried casting (property.PropertyType)variable but this also doesn't work.

how can I do this? if there is another way other than reflections do tell please.

thanks for helping





Aucun commentaire:

Enregistrer un commentaire