Skip to content

Instantly share code, notes, and snippets.

@jehugaleahsa
Last active August 29, 2015 14:02
Show Gist options
  • Save jehugaleahsa/673596b34929974a27c7 to your computer and use it in GitHub Desktop.
Save jehugaleahsa/673596b34929974a27c7 to your computer and use it in GitHub Desktop.
private static T getValue<T>(object source, IFormatProvider provider)
{
Type type = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);
if (source == null)
{
if (typeof(T).IsValueType && typeof(T) == type)
{
throw new InvalidCastException();
}
return default(T);
}
if (source is IConvertible)
{
source = System.Convert.ChangeType(source, type, provider);
}
return (T)source;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment