Consider using the TypeConverter and generic methods. This avoids lots of if statement. Please add your own error handling based on MSDN documentation
class Program
{
static T convert(string s)
{
var typeConverter = TypeDescriptor.GetConverter(typeof(T));
if (typeConverter != null && typeConverter.CanConvertFrom(typeof(string)))
{
return (T) typeConverter.ConvertFrom(s);
}
return default(T);
}
static void Main(string[] args)
{
int x = convert( "45");
}
}