In addition to ToInt32() and all other parsing things. System.Convert should have generic parser
public static bool TryParse<T>(string s,out T v) where T : struct,IConvertible,IFormattable,IComparable<T>,IEquatable<T>
{
return T.TryParse(s,out v);
}
public static T ChangeType<T>(object obj) where T : IConvertible
{
T v;
ChangeType<T>(obj,out v);
return v;
}
public static bool ChangeType<T>(object obj,out T v) where T : IConvertible
{
try
{
/// Should be better algorithm instead of try/catch + unboxing
v = (T)Convert.ChangeType(obj,typeof(T));
return true;
}
catch
{
v = default(T);
return false;
}
}
/// usage
int i;
if(Convert.TryParse("123456",out i))
DoSomething(i);
double d;
if(Convert.ChangeType(i,out d))
DoSomething(d);
In addition to ToInt32() and all other parsing things. System.Convert should have generic parser