RestImageResize/Utils/SmartConvert.cs
/// <summary>
/// Attempts to convert value to the equivalent value of specified type.
/// </summary>
/// <param name="targetType">The target conversion type.</param>
/// <param name="value">The value to convert.</param>
/// <param name="defaultValue">The default value of target type that should be used as empty value equivalent.</param>
/// <param name="fallbackValue">The value that should be used if source value can not be converted to target type (if not specified <paramref name="defaultValue"/> is used).</param>
/// <returns>
/// The conversion result value.
/// </returns>
Attempts to convert value to the equivalent value of specified type.
Attempts to convert value to the equivalent value of specified type .
public static object TryChangeType(Type targetType, object value, object defaultValue = null, object fallbackValue = null)
        {
            if (targetType == null)
            {
                throw new ArgumentNullException("targetType");
            }
            if (defaultValue != null)
            {
                try
                {
                    defaultValue = ChangeType(targetType, defaultValue);
                }
                catch (Exception ex)
                {
                    throw new ArgumentException("Default value type mismatch.", "defaultValue", ex);
                }
            }
            if (fallbackValue != null)
            {
                try
                {
                    fallbackValue = ChangeType(targetType, fallbackValue);
                }
                catch (Exception ex)
                {
                    throw new ArgumentException("Fallback value type mismatch.", "fallbackValue", ex);
                }
            }
            try
            {
                return ChangeTypeImpl(targetType, value, defaultValue);
            }
            catch
            {
                if (fallbackValue != null)
                {
                    return fallbackValue;
                }
                return GetNullValue(targetType);
            }
        }
public static object TryChangeType ( Type targetType , object value , object defaultValue = null , object fallbackValue = null ) { if ( targetType == null ) { throw new ArgumentNullException ( " " ) ; } if ( defaultValue != null ) { try { defaultValue = ChangeType ( targetType , defaultValue ) ; } catch ( Exception ex ) { throw new ArgumentException ( " " , " " , ex ) ; } } if ( fallbackValue != null ) { try { fallbackValue = ChangeType ( targetType , fallbackValue ) ; } catch ( Exception ex ) { throw new ArgumentException ( " " , " " , ex ) ; } } try { return ChangeTypeImpl ( targetType , value , defaultValue ) ; } catch { if ( fallbackValue != null ) { return fallbackValue ; } return GetNullValue ( targetType ) ; } }
Attempts to convert value to the equivalent value of specified type.
Attempts to convert value to the equivalent value of specified type .
The conversion result value.
The conversion result value .
targetType
Type
The target conversion type.
The target conversion type .
value
object
The value to convert.
The value to convert .
defaultValue
object
The default value of target type that should be used as empty value equivalent.
The default value of target type that should be used as empty value equivalent .
fallbackValue
object
The value that should be used if source value can not be converted to target type (if not specified  is used).
The value that should be used if source value can not be converted to target type ( if not specified is used ) .