mardi 22 août 2017

How to Convert Types Using Reflection

Please consider this Code:

string propertyValue = "1";
PropertyInfo info = obj.GetType().GetProperty("MyProperty");
info.SetValue(detail, Convert.ChangeType(propertyValue, info.PropertyType), null);

The problem is type of info.PropertyType is System.Byte? and when the line 3 wants to be executed I got this Error:

"Invalid cast from 'System.String' to 'System.Nullable`1[[System.Byte, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'."}

How I resolve this problem?

Thanks





Aucun commentaire:

Enregistrer un commentaire