// Converts the value of a single element to a desired type. private static object CreateObjectFromString(string value, Type dstType) { var underlyingType = Nullable.GetUnderlyingType(dstType); if (underlyingType != null) { if (string.IsNullOrEmpty(value)) { return(null); // Returns Nullable<T>(). } // Otherwise, continue with our conversion using // the underlying type of the nullable. dstType = underlyingType; } var converter = Configuration.FindTypeStringConverter(dstType); try { return(converter.ConvertFromString(value, dstType)); } catch (Exception ex) { throw SettingValueCastException.Create(value, dstType, ex); } }
// Converts the value of a single element to a desired type. private static object CreateObjectFromString(string value, Type dstType) { var underlyingType = Nullable.GetUnderlyingType(dstType); if (underlyingType != null) { if (string.IsNullOrEmpty(value)) { return(null); // Returns Nullable<T>(). } // Otherwise, continue with our conversion using // the underlying type of the nullable. dstType = underlyingType; } var converter = Configuration.FindTypeStringConverter(dstType); if (converter == Configuration.FallbackConverter) { // The fallback converter is not able to create arbitrary objects // from strings, so this means that there is no type converter // registered for dstType. throw SettingValueCastException.CreateBecauseConverterMissing(value, dstType); } try { return(converter.ConvertFromString(value, dstType)); } catch (Exception ex) { throw SettingValueCastException.Create(value, dstType, ex); } }
public string ConvertToString(object value) { try { var converter = TypeDescriptor.GetConverter(value); return(converter.ConvertToString(null, Configuration.CultureInfo, value)); } catch (Exception ex) { throw SettingValueCastException.Create(value.ToString(), value.GetType(), ex); } }
public object ConvertFromString(string value, Type hint) { try { var converter = TypeDescriptor.GetConverter(hint); return(converter.ConvertFrom(null, Configuration.CultureInfo, value)); } catch (Exception ex) { throw SettingValueCastException.Create(value, hint, ex); } }
public override object ConvertFromString(string value, Type hint) { switch (value.ToLowerInvariant()) { case "false": case "off": case "no": case "n": case "0": return(false); case "true": case "on": case "yes": case "y": case "1": return(true); } throw SettingValueCastException.Create(value, hint, null); }
public override object ConvertFromString(string value, Type hint) { return(TryConvertFromString(value, hint) ?? throw SettingValueCastException.Create(value, hint, null)); }