1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| internal static T GetValue<T>(this string s) {
TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
if (converter == null)
throw new Exception("converter for type '" + typeof(T).FullName + "' not found");
if (string.Equals("null", s) || string.Equals("NULL", s) || string.IsNullOrEmpty(s) || string.IsNullOrWhiteSpace(s))
return default(T);
if ((typeof(T) == typeof(double?)) || (typeof(T) == typeof(double)))
s = s.Replace('.', ',');
else if (typeof(T) == typeof(bool?))
s = ((s == "no" || s == "No" || s == "NO") ? "false" : ((s == "yes" || s == "Yes" || s == "YES") ? "true" : "null"));
else if (typeof(T) == typeof(bool))
s = ((s == "yes" || s == "Yes" || s == "YES") ? "true" : "false");
return (T)converter.ConvertFrom(s);
} |
Partager