Example #1
0
        /// <inheritdoc />
        protected override TNumberTo ConvertValue(TNumberFrom value, TNumberTo defaultValue, IFormatProvider format, IConversionObserver conversionObserver)
        {
            TNumberTo minValue, maxValue;

            if (this.IsNumericOverflow(value, out minValue, out maxValue))
            {
                if (conversionObserver != null)
                {
                    conversionObserver.NotifyKnownFallbackToDefaultValue(
                        value: value,
                        targetType: typeof(TNumberTo),
                        defaultTargetValue: defaultValue,
                        format: format,
                        fallbackReason: new OverflowException(
                            string.Format(
                                "The passed value (type {0}, value: {1}) exceeds the target numeric type range (target type: {2}, min-value: {3}, max-value: {4})",
                                typeof(TNumberFrom),
                                value,
                                typeof(TNumberTo),
                                minValue,
                                maxValue)));
                }

                return(defaultValue);
            }

            TNumberTo castedValue;

            if (this.TryCastToNumber(value, out castedValue))
            {
                return(castedValue);
            }
            else
            {
                if (conversionObserver != null)
                {
                    conversionObserver.NotifyKnownFallbackToDefaultValue(
                        value: value,
                        targetType: typeof(TNumberTo),
                        defaultTargetValue: defaultValue,
                        format: format,
                        fallbackReason: new InvalidCastException(
                            string.Format(
                                "The passed value (type {0}, value: {1}) cannot be casted to the specified target numeric type({2})",
                                typeof(TNumberFrom),
                                value,
                                typeof(TNumberTo))));
                }

                return(defaultValue);
            }
        }
Example #2
0
        /// <inheritdoc />
        protected override char ConvertValue(string value, char defaultValue, IFormatProvider format, IConversionObserver conversionObserver)
        {
            var charArray = this.GetCharArray(value);

            if ((charArray != null) && (charArray.Length > 0))
            {
                return(charArray[0]);
            }

            if (conversionObserver != null)
            {
                conversionObserver.NotifyKnownFallbackToDefaultValue(
                    value: value,
                    targetType: typeof(char),
                    defaultTargetValue: defaultValue,
                    format: format,
                    fallbackReason: new ArgumentException(
                        string.Format(
                            "The specified string '{0}' can't be split into a non-zero length char array, using specified default value ('{1}') instead",
                            value,
                            defaultValue)));
            }

            return(defaultValue);
        }
        /// <inheritdoc />
        protected override bool ConvertValue(string value, bool defaultValue, IFormatProvider format, IConversionObserver conversionObserver)
        {
            if (this.IsTrueishValue(value))
            {
                return(true);
            }

            if (this.IsFalseishValue(value))
            {
                return(false);
            }

            if (conversionObserver != null)
            {
                conversionObserver.NotifyKnownFallbackToDefaultValue(
                    value: value,
                    targetType: typeof(bool),
                    defaultTargetValue: defaultValue,
                    format: format,
                    fallbackReason: new FormatException(
                        string.Format(
                            "The specified string '{0}' doesn't represent a true-ish or false-ish value, returning specified default value ('{1}') instead",
                            value,
                            defaultValue)));
            }

            return(defaultValue);
        }
        /// <inheritdoc />
        protected override TNumber ConvertValue(string value, TNumber defaultValue, IFormatProvider format, IConversionObserver conversionObserver)
        {
            TNumber parseResult;

            if (this.TryParseNumber(value, this.GetParseNumberStyles(), format, out parseResult))
            {
                return(parseResult);
            }

            if (conversionObserver != null)
            {
                conversionObserver.NotifyKnownFallbackToDefaultValue(
                    value: value,
                    targetType: typeof(TNumber),
                    defaultTargetValue: defaultValue,
                    format: format,
                    fallbackReason: new FormatException(
                        string.Format(
                            "The passed string '{0}' format is not a parsable numeric (of type {1}) value",
                            value,
                            typeof(TNumber))));
            }

            return(defaultValue);
        }
Example #5
0
        /// <inheritdoc />
        protected override Guid ConvertValue(string value, Guid defaultValue, IFormatProvider format, IConversionObserver conversionObserver)
        {
            Guid parseResult;

            if (Guid.TryParse(value, out parseResult))
            {
                return(parseResult);
            }

            if (conversionObserver != null)
            {
                conversionObserver.NotifyKnownFallbackToDefaultValue(
                    value: value,
                    targetType: typeof(Guid),
                    defaultTargetValue: defaultValue,
                    format: format,
                    fallbackReason: new FormatException(
                        string.Format(
                            "The passed string '{0}' format is not a parsable Guid value",
                            value)));
            }

            return(defaultValue);
        }
Example #6
0
        /// <inheritdoc />
        protected override DateTime ConvertValue(string value, DateTime defaultValue, IFormatProvider format, IConversionObserver conversionObserver)
        {
            DateTime parsedDateTime;

            if (this.TryParseDateTime(value, this.GetParseDateTimeStyles(), format, out parsedDateTime))
            {
                return(parsedDateTime);
            }

            if (conversionObserver != null)
            {
                conversionObserver.NotifyKnownFallbackToDefaultValue(
                    value: value,
                    targetType: typeof(DateTime),
                    defaultTargetValue: defaultValue,
                    format: format,
                    fallbackReason: new FormatException(
                        string.Format(
                            "The passed string '{0}' format is not a parsable DateTime value",
                            value)));
            }

            return(defaultValue);
        }
		/// <summary>
		/// Converts a value to the specified type
		/// </summary>
		/// <param name="value">The value to convert</param>
		/// <param name="targetType">The type to which the value should be converted</param>
		/// <param name="targetDefaultValue">The default value to use instead of null or DBNull.Value</param>
		/// <param name="format">The format used for conversion</param>
		/// <param name="conversionObserver">Conversion observer</param>
		/// <returns>The converted value</returns>
		public static object Convert(object value, Type targetType, object targetDefaultValue, IFormatProvider format, IConversionObserver conversionObserver)
		{
			if (targetType == null)
			{
				throw new ArgumentNullException("targetType");
			}

			TypeInfo targetTypeInfo = targetType.GetTypeInfo();
			if (targetTypeInfo == null)
			{
				throw new ArgumentException(string.Format("Unable to get type information for type {0}", targetType));
			}

			if (targetTypeInfo.IsValueType && ((targetDefaultValue == null) || (ConversionsHelper.IsDBNull(targetDefaultValue))))
			{
				// converting to a value type, but having the default value as null / DBNull
				targetDefaultValue = ConversionsHelper.GetDefaultValue(targetType);
			}

			if ((value == null) || ConversionsHelper.IsDBNull(value) || ConversionsHelper.IsStringEmptyOrWhitespace(value))
			{
				// converting null / DBNull / empty or whitespaces-only string to target type => just return default value
				if(conversionObserver != null)
				{
					conversionObserver.NotifyKnownFallbackToDefaultValue(
						value: value,
						targetType: targetType,
						defaultTargetValue: targetDefaultValue,
						format: format,
						fallbackReason: new ArgumentNullException("value"));
				}

				return targetDefaultValue;
			}

			object converted = targetDefaultValue;

			try
			{
				// Handle Nullable<T>: try to convert to underlying T
				bool targetIsGenericType = ConversionsHelper.IsGenericType(targetTypeInfo);
				if (targetIsGenericType)
				{
					Type nullableTypeArg;
					if (ConversionsHelper.TryParseNullableType(targetType, out nullableTypeArg))
					{
						targetType = nullableTypeArg;
					}
				}

				var bootstrapper = Converter.Settings.SpecializedConversionsBootstrapper;
				if (bootstrapper == null)
				{
					bootstrapper = new DefaultSpecializedConversionsBootstrapper();
				}

				var converter = SpecializedConverterLocator.GetSpecializedConverter(value.GetType(), targetType, bootstrapper.DiscoveredSpecializedConverters);
				if (converter == null)
				{
					converter = new GenericConverter(targetType);
				}

				converted = converter.Convert(value, targetDefaultValue, format, conversionObserver);
			}
			catch (Exception ex)
			{
				if (
					// Convert.ChangeType may throw InvalidCastException when:
					//   - This conversion is not supported OR
					//   - value is null and conversionType is a value type OR
					//   - value does not implement the IConvertible interface
					(ex is InvalidCastException) ||

					// Convert.ChangeType may throw FormatException when :
					//   - value is not in a format for conversionType recognized by provider.
					(ex is FormatException) ||

					// Convert.ChangeType may throw OverflowException when:
					//   - value represents a number that is out of the range of conversionType.
					// - or -
					// Enum.Parse may throw OverflowException when:
					//   - value is outside the range of the underlying type of enumType
					(ex is OverflowException) ||

					// Convert.ChangeType may throw ArgumentNullException when :
					//   - conversionType is null.
					// - or -
					// Enum.IsDefined may throw System.ArgumentNullException when:
					//   - enumType or value is null
					// - or -
					// Enum.Parse may throw ArgumentNullException when :
					//   - enumType or value is null.
					(ex is ArgumentNullException) ||

					// Enum.IsDefined may throw System.ArgumentException when:
					//   - enumType is not an Enum.
					//   - The type of value is an enumeration, but it is not an enumeration of type enumType.
					//   - The type of value is not an underlying type of enumType.
					// - or -
					// Enum.Parse may throw ArgumentException when:
					//   - enumType is not an Enum
					//   - value is either an empty string or only contains white space
					//   - value is a name, but not one of the named constants defined for the enumeration
					(ex is ArgumentException) ||

					// Enum.IsDefined may throwSystem.InvalidOperationException when:
					//   - value is not type System.SByte, System.Int16, System.Int32, System.Int64, System.Byte, System.UInt16, System.UInt32, or System.UInt64, or System.String.
					(ex is InvalidOperationException))
				{
					if (conversionObserver != null)
					{
						conversionObserver.NotifyCaughtConversionException(
							value: value,
							targetType: targetType,
							defaultTargetValue: targetDefaultValue,
							format: format,
							conversionException: ex);
					}
				}
				else
				{
					throw;
				}
			}

			return converted;
		}
        /// <summary>
        /// Converts a value to the specified type
        /// </summary>
        /// <param name="value">The value to convert</param>
        /// <param name="targetType">The type to which the value should be converted</param>
        /// <param name="targetDefaultValue">The default value to use instead of null or DBNull.Value</param>
        /// <param name="format">The format used for conversion</param>
        /// <param name="conversionObserver">Conversion observer</param>
        /// <returns>The converted value</returns>
        public static object Convert(object value, Type targetType, object targetDefaultValue, IFormatProvider format, IConversionObserver conversionObserver)
        {
            if (targetType == null)
            {
                throw new ArgumentNullException("targetType");
            }

            TypeInfo targetTypeInfo = targetType.GetTypeInfo();

            if (targetTypeInfo == null)
            {
                throw new ArgumentException(string.Format("Unable to get type information for type {0}", targetType));
            }

            if (targetTypeInfo.IsValueType && ((targetDefaultValue == null) || (ConversionsHelper.IsDBNull(targetDefaultValue))))
            {
                // converting to a value type, but having the default value as null / DBNull
                targetDefaultValue = ConversionsHelper.GetDefaultValue(targetType);
            }

            if ((value == null) || ConversionsHelper.IsDBNull(value) || ConversionsHelper.IsStringEmptyOrWhitespace(value))
            {
                // converting null / DBNull / empty or whitespaces-only string to target type => just return default value
                if (conversionObserver != null)
                {
                    conversionObserver.NotifyKnownFallbackToDefaultValue(
                        value: value,
                        targetType: targetType,
                        defaultTargetValue: targetDefaultValue,
                        format: format,
                        fallbackReason: new ArgumentNullException("value"));
                }

                return(targetDefaultValue);
            }

            object converted = targetDefaultValue;

            try
            {
                // Handle Nullable<T>: try to convert to underlying T
                bool targetIsGenericType = ConversionsHelper.IsGenericType(targetTypeInfo);
                if (targetIsGenericType)
                {
                    Type nullableTypeArg;
                    if (ConversionsHelper.TryParseNullableType(targetType, out nullableTypeArg))
                    {
                        targetType = nullableTypeArg;
                    }
                }

                var bootstrapper = Converter.Settings.SpecializedConversionsBootstrapper;
                if (bootstrapper == null)
                {
                    bootstrapper = new DefaultSpecializedConversionsBootstrapper();
                }

                var converter = SpecializedConverterLocator.GetSpecializedConverter(value.GetType(), targetType, bootstrapper.DiscoveredSpecializedConverters);
                if (converter == null)
                {
                    converter = new GenericConverter(targetType);
                }

                converted = converter.Convert(value, targetDefaultValue, format, conversionObserver);
            }
            catch (Exception ex)
            {
                if (
                    // Convert.ChangeType may throw InvalidCastException when:
                    //   - This conversion is not supported OR
                    //   - value is null and conversionType is a value type OR
                    //   - value does not implement the IConvertible interface
                    (ex is InvalidCastException) ||

                    // Convert.ChangeType may throw FormatException when :
                    //   - value is not in a format for conversionType recognized by provider.
                    (ex is FormatException) ||

                    // Convert.ChangeType may throw OverflowException when:
                    //   - value represents a number that is out of the range of conversionType.
                    // - or -
                    // Enum.Parse may throw OverflowException when:
                    //   - value is outside the range of the underlying type of enumType
                    (ex is OverflowException) ||

                    // Convert.ChangeType may throw ArgumentNullException when :
                    //   - conversionType is null.
                    // - or -
                    // Enum.IsDefined may throw System.ArgumentNullException when:
                    //   - enumType or value is null
                    // - or -
                    // Enum.Parse may throw ArgumentNullException when :
                    //   - enumType or value is null.
                    (ex is ArgumentNullException) ||

                    // Enum.IsDefined may throw System.ArgumentException when:
                    //   - enumType is not an Enum.
                    //   - The type of value is an enumeration, but it is not an enumeration of type enumType.
                    //   - The type of value is not an underlying type of enumType.
                    // - or -
                    // Enum.Parse may throw ArgumentException when:
                    //   - enumType is not an Enum
                    //   - value is either an empty string or only contains white space
                    //   - value is a name, but not one of the named constants defined for the enumeration
                    (ex is ArgumentException) ||

                    // Enum.IsDefined may throwSystem.InvalidOperationException when:
                    //   - value is not type System.SByte, System.Int16, System.Int32, System.Int64, System.Byte, System.UInt16, System.UInt32, or System.UInt64, or System.String.
                    (ex is InvalidOperationException))
                {
                    if (conversionObserver != null)
                    {
                        conversionObserver.NotifyCaughtConversionException(
                            value: value,
                            targetType: targetType,
                            defaultTargetValue: targetDefaultValue,
                            format: format,
                            conversionException: ex);
                    }
                }
                else
                {
                    throw;
                }
            }

            return(converted);
        }