C# Generic TryParse Extension method

public static T? TryParse<T>(this object obj) where T : struct
{
if (obj == null) return null;

T? result = null;
TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
if (converter != null)
{
try
{
// result = (T)converter.ConvertFrom(obj);
string str = obj.ToString();
result = (T)converter.ConvertFromString(str);
}
catch (Exception)
{
}
}
return result;
}
To use this baby:
decimal? d = obj.TryParse<decimal>();
int? i = obj.TryParse<int>();
DateTime? dt = obj.TryParse<DateTime>();
object = null.TryParse<string>();
here's some more craziness...
 
public static T? TryParse<T>(this object obj, Func<Exception, T?> repairFunc) where T : struct
// repairFunc to handle what happens during an exception.