Skip to content

Instantly share code, notes, and snippets.

@jonathascosta
Created May 23, 2011 21:20
Show Gist options
  • Save jonathascosta/987644 to your computer and use it in GitHub Desktop.
Save jonathascosta/987644 to your computer and use it in GitHub Desktop.
String Extensions
public static class TypeExtensions
{
public static bool IsNullableOrType<T>(this Type type)
where T : struct
{
return (type == typeof(T) || type == typeof(T?));
}
}
public static class StringExtensions
{
public static object ToType(this string origin, Type dest)
{
if (dest.IsNullableOrType<DateTime>())
return DateTime.Parse(origin);
if (dest.IsNullableOrType<decimal>())
return decimal.Parse(origin);
if (dest.IsNullableOrType<int>())
return int.Parse(origin);
if (dest.IsNullableOrType<bool>())
return bool.Parse(origin);
if (dest.IsNullableOrType<long>())
return long.Parse(origin);
if (dest.IsNullableOrType<short>())
return short.Parse(origin);
if (dest.IsNullableOrType<float>())
return float.Parse(origin);
if (dest.IsNullableOrType<double>())
return double.Parse(origin);
if (dest.IsNullableOrType<byte>())
return byte.Parse(origin);
return origin;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment