have you any alternative to these code, i want a more generic code I tried Convert class but with no success
public object convert(Type type, string value)
{
object r = null;
if (type == typeof(bool))
{
r = bool.Parse(value);
}
else if (type == typeof(int))
{
r = int.Parse(value);
}
else if (type == typeof(string))
{
r = value;
}
return r;
}
var conv = TypeDescriptor.GetConverter(type);
return conv.ConvertFromInvariantString(value);
Other conversion operations exist if you don't want "invariant". It depends on your needs. See also ConvertFromString if you want locale settins to apply, etc.
You mention you have tried the Convert class, but have you also tried Convert.ChangeType(value, type)? What where the problems you ran in to?
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