Last active
August 29, 2015 14:02
-
-
Save jehugaleahsa/673596b34929974a27c7 to your computer and use it in GitHub Desktop.
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
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