Created
May 23, 2011 21:20
-
-
Save jonathascosta/987644 to your computer and use it in GitHub Desktop.
String Extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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