// 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); } }
/// <summary> /// Sets the value of this setting via an array. /// </summary> /// /// <param name="values">The values to set.</param> public void SetValue(object[] values) { if (values == null) { SetEmptyValue(); } else { if (values.GetType().GetElementType().IsArray) { throw new ArgumentException("Jagged arrays are not supported."); } var strings = new string[values.Length]; for (int i = 0; i < values.Length; ++i) { var converter = Configuration.FindTypeStringConverter(values[i].GetType()); strings[i] = converter.ConvertToString(values[i]); } mRawValue = string.Format("{{{0}}}", string.Join(",", strings)); mCachedArraySize = values.Length; mShouldCalculateArraySize = false; } }
/// <summary> /// Sets the value of this setting via an object. /// </summary> /// /// <param name="value">The value to set.</param> public void SetValue <T>(T value) { if (value == null) { SetEmptyValue(); } else { var converter = Configuration.FindTypeStringConverter(typeof(T)); mRawValue = converter.ConvertToString(value); mShouldCalculateArraySize = true; } }
/// <summary> /// Sets the value of this setting via an object. /// </summary> /// /// <param name="value">The value to set.</param> public void SetValue(object value) { if (value == null) { SetEmptyValue(); return; } var type = value.GetType(); if (type.IsArray) { var elementType = type.GetElementType(); if (elementType != null && elementType.IsArray) { throw CreateJaggedArraysNotSupportedEx(type.GetElementType()); } var values = value as Array; if (values != null) { var strings = new string[values.Length]; for (int i = 0; i < values.Length; i++) { object elemValue = values.GetValue(i); var converter = Configuration.FindTypeStringConverter(elemValue.GetType()); strings[i] = GetValueForOutput(converter.ConvertToString(elemValue)); } mRawValue = $"{{{string.Join(Configuration.ArrayElementSeparator.ToString(), strings)}}}"; } if (values != null) { mCachedArraySize = values.Length; } mShouldCalculateArraySize = false; } else { var converter = Configuration.FindTypeStringConverter(type); mRawValue = converter.ConvertToString(value); mShouldCalculateArraySize = true; } }
/// <summary> /// Sets the value of this setting via an object. /// </summary> /// /// <param name="value">The value to set.</param> public void SetValue(object value) { if (value == null) { SetEmptyValue(); return; } var type = value.GetType(); if (type.IsArray) { if (type.GetElementType().IsArray) { throw new ArgumentException("Jagged arrays are not supported."); } var values = value as Array; var strings = new string[values.Length]; for (int i = 0; i < values.Length; i++) { object elemValue = values.GetValue(i); var converter = Configuration.FindTypeStringConverter(elemValue.GetType()); strings[i] = converter.ConvertToString(elemValue); } mRawValue = string.Format("{{{0}}}", string.Join(",", strings)); mCachedArraySize = values.Length; mShouldCalculateArraySize = false; } else { var converter = Configuration.FindTypeStringConverter(type); mRawValue = converter.ConvertToString(value); mShouldCalculateArraySize = true; } }