Skip to content

Instantly share code, notes, and snippets.

@juanplopes
Forked from jonathascosta/gist:987644
Created May 23, 2011 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juanplopes/987646 to your computer and use it in GitHub Desktop.
Save juanplopes/987646 to your computer and use it in GitHub Desktop.
String Extensions
public static class TypeExtensions
{
public static Type GetValueTypeIfNullable(this Type type)
{
return (type.IsGenericType && typeof(Nullable<>) == type.GetGenericTypeDefinition())
? type.GetGenericArguments()[0] : type;
}
}
public static class StringExtensions
{
public static object ToType(this string origin, Type dest)
{
var realType = type.GetValueTypeIfNullable();
if (typeof(IConvertible).IsAssignableFrom(realType))
return Convert.ChangeType(origin, realType);
return origin;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment