RestImageResize/Utils/SmartConvert.cs
/// <summary>
/// Converts 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>
/// <returns>
/// The conversion result value.
/// </returns>
Converts value to the equivalent value of specified type.
Converts value to the equivalent value of specified type .
private static object ChangeTypeImpl(Type targetType, object value, object defaultValue = null)
        {
            if (object.Equals(value, string.Empty))
            {
                if (targetType == typeof(string))
                {
                    return value; 
                }
                value = null;
            }
            if (value == null || value == GetNullValue(value.GetType()))
            {
                if (defaultValue != null)
                {
                    return defaultValue; 
                }
                return GetNullValue(targetType); 
            }
            targetType = Nullable.GetUnderlyingType(targetType) ?? targetType;
            Type sourceType = value.GetType();
            sourceType = Nullable.GetUnderlyingType(sourceType) ?? sourceType;
            if (targetType.IsAssignableFrom(sourceType))
            {
                return value; 
            }
            if (targetType == typeof(Boolean))
            {
                if (sourceType == typeof(String))
                {
                    return ConvertStringToBool((String)value); 
                }
                if (sourceType == typeof(int))
                {
                    return ConvertIntToBool((int)value); 
                }
            }
            if (targetType == typeof(Guid) && sourceType == typeof(string))
            {
                return Guid.Parse((string)value);
            }
            if (targetType.IsEnum)
            {
                string strValue = value as string;
                if (strValue != null)
                {
                    return Enum.Parse(targetType, strValue, true); 
                }
                return Enum.ToObject(targetType, value); 
            }
            Type soursEnumerableType =
                (sourceType.IsGenericType && sourceType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ?
                sourceType :
                sourceType.GetInterface(typeof(IEnumerable).FullName, false);
            if (soursEnumerableType != null)
            {
                Type targetCollectionType =
                    (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ?
                    targetType :
                    targetType.GetInterface(typeof(IEnumerable<>).FullName, false);
                if (targetCollectionType != null)
                {
                    Type targetItemType = targetCollectionType.GetGenericArguments()[0];
                    if (targetType.IsArray || (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
                    {
                        var itemsList = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(new[] { targetItemType }));
                        foreach (object item in (IEnumerable)value)
                        {
                            var convertedItem = ChangeType(targetItemType, item);
                            itemsList.Add(convertedItem);
                        }
                        MethodInfo toArrayMethod = typeof(Enumerable).GetMethod("ToArray", BindingFlags.Static | BindingFlags.Public);
                        toArrayMethod = toArrayMethod.MakeGenericMethod(new[] { targetItemType });
                        return toArrayMethod.Invoke(null, new object[] { itemsList }); 
                    }
                    var targetCollection = Activator.CreateInstance(targetType);
                    MethodInfo addMethod = targetType.GetMethod("Add", new[] { targetItemType });
                    foreach (object item in (IEnumerable)value)
                    {
                        var convertedItem = ChangeType(targetItemType, item);
                        addMethod.Invoke(targetCollection, new[] { convertedItem });
                    }
                    return targetCollection; 
                }
            }
            return Convert.ChangeType(value, targetType); 
        }
private static object ChangeTypeImpl ( Type targetType , object value , object defaultValue = null ) { if ( object . Equals ( value , string . Empty ) ) { if ( targetType == typeof ( string ) ) { return value ; } value = null ; } if ( value == null || value == GetNullValue ( value . GetType ( ) ) ) { if ( defaultValue != null ) { return defaultValue ; } return GetNullValue ( targetType ) ; } targetType = Nullable . GetUnderlyingType ( targetType ) ?? targetType ; Type sourceType = value . GetType ( ) ; sourceType = Nullable . GetUnderlyingType ( sourceType ) ?? sourceType ; if ( targetType . IsAssignableFrom ( sourceType ) ) { return value ; } if ( targetType == typeof ( Boolean ) ) { if ( sourceType == typeof ( String ) ) { return ConvertStringToBool ( ( String ) value ) ; } if ( sourceType == typeof ( int ) ) { return ConvertIntToBool ( ( int ) value ) ; } } if ( targetType == typeof ( Guid ) && sourceType == typeof ( string ) ) { return Guid . Parse ( ( string ) value ) ; } if ( targetType . IsEnum ) { string strValue = value as string ; if ( strValue != null ) { return Enum . Parse ( targetType , strValue , true ) ; } return Enum . ToObject ( targetType , value ) ; } Type soursEnumerableType = ( sourceType . IsGenericType && sourceType . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > ) ) ? sourceType : sourceType . GetInterface ( typeof ( IEnumerable ) . FullName , false ) ; if ( soursEnumerableType != null ) { Type targetCollectionType = ( targetType . IsGenericType && targetType . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > ) ) ? targetType : targetType . GetInterface ( typeof ( IEnumerable < > ) . FullName , false ) ; if ( targetCollectionType != null ) { Type targetItemType = targetCollectionType . GetGenericArguments ( ) [ 0 ] ; if ( targetType . IsArray || ( targetType . IsGenericType && targetType . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > ) ) ) { var itemsList = ( IList ) Activator . CreateInstance ( typeof ( List < > ) . MakeGenericType ( new [ ] { targetItemType } ) ) ; foreach ( object item in ( IEnumerable ) value ) { var convertedItem = ChangeType ( targetItemType , item ) ; itemsList . Add ( convertedItem ) ; } MethodInfo toArrayMethod = typeof ( Enumerable ) . GetMethod ( " " , BindingFlags . Static | BindingFlags . Public ) ; toArrayMethod = toArrayMethod . MakeGenericMethod ( new [ ] { targetItemType } ) ; return toArrayMethod . Invoke ( null , new object [ ] { itemsList } ) ; } var targetCollection = Activator . CreateInstance ( targetType ) ; MethodInfo addMethod = targetType . GetMethod ( " " , new [ ] { targetItemType } ) ; foreach ( object item in ( IEnumerable ) value ) { var convertedItem = ChangeType ( targetItemType , item ) ; addMethod . Invoke ( targetCollection , new [ ] { convertedItem } ) ; } return targetCollection ; } } return Convert . ChangeType ( value , targetType ) ; }
Converts value to the equivalent value of specified type.
Converts value to the equivalent value of specified type .
//------------------------------------------RETURN---------------------------------------// //------------------------------------------RETURN---------------------------------------// //------------------------------------------RETURN---------------------------------------// //------------------------------------------RETURN---------------------------------------// //------------------------------------------RETURN---------------------------------------// //------------------------------------------RETURN---------------------------------------// //------------------------------------------RETURN---------------------------------------// //------------------------------------------RETURN---------------------------------------// // ReSharper disable AssignNullToNotNullAttribute // ReSharper restore AssignNullToNotNullAttribute // ReSharper disable AssignNullToNotNullAttribute // ReSharper restore AssignNullToNotNullAttribute //------------------------------------------RETURN---------------------------------------// //------------------------------------------RETURN---------------------------------------// //------------------------------------------RETURN---------------------------------------//
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 .