I have a method which converts DataTable to List<T>. It was working fine until I had bit column in my MySql database. It was unable to convert bit value 1 to C# type bool. So I tried to convert it like
Convert.ChangeType(value, prop.PropertyType);
where value is something that database returned and prop in PropertyInfo where value will be assigned.
It worked fine but it broke how enums were added.
e.g. previously I was able to assign integer to enum field but now getting an error
Invalid cast from 'System.Int32' to 'EnumsAndConstants.QuestionType'.
I know one solution could be converting value to type only when TypeCastingException occurs but I don't want exception to occur at all. Is there any concrete solution that work for all types?
Aucun commentaire:
Enregistrer un commentaire