I have a generic method in a MySQL connector witch make generic conversion. I simplify it for the question, cause it makes lot of things. At the end, it make a generic conversion and work fine, but I have a problem to convert Int64 to Int32.
object var1 = (long)64;
int? var2 = Select<int?>(var1); // ERROR Int32Converter from Int64
public T Select<T>(object value)
{
return (T)System.ComponentModel.TypeDescriptor.GetConverter(typeof(T)).ConvertFrom(value);
}
How can I fix it ?
Can't you use Convert.ChangeType instead?
int value = Convert.ChangeType(var1, TypeCode.Int32);
or
int value = Convert.ToInt32(var1);
note that these will throw exceptions if var is outside the allowable range of Int32
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With