示例#1
0
        public static object ConvertTo(object value, Type targetType, CultureInfo culture = null)
        {
            if (value == null)
            {
                return(ChoConvert.ConvertTo(value, targetType, value, (object[])null, (object[])null, culture));
            }

            Type origType = targetType;
            Type type     = value == null ? typeof(object) : value.GetType();

            if (type == origType)
            {
                return(value);
            }

            return(ChoConvert.ConvertTo(value, targetType, value, ChoTypeDescriptor.GetTypeConvertersForType(type), null, culture));
        }
示例#2
0
        public static object ConvertFrom(object value, Type targetType, object sourceObject = null, object[] converters = null, object[] parameters = null, CultureInfo culture = null)
        {
            Type origType = targetType;

            targetType = targetType.IsNullableType() ? targetType.GetUnderlyingType() : targetType;
            object obj1 = value;

            if (targetType == (Type)null)
            {
                return(value);
            }
            if (targetType == typeof(object))
            {
                return(value);
            }
            if (culture == null)
            {
                culture = ChoConvert.DefaultCulture;
            }

            if (value is ICollection && !typeof(ICollection).IsAssignableFrom(targetType))
            {
                value = ((IEnumerable)value).FirstOrDefault <object>();
            }
            if (typeof(IList).IsAssignableFrom(targetType) && !(value is IList))
            {
                value = new object[] { value };
            }

            Type type = value == null ? typeof(object) : value.GetType();

            try
            {
                if (converters.IsNullOrEmpty())
                {
                    converters = ChoTypeDescriptor.GetTypeConvertersForType(targetType);
                }

                if (converters != null && converters.Length > 0)
                {
                    object[] objArray = (object[])null;
                    for (int index = 0; index < converters.Length; ++index)
                    {
                        object obj2 = converters[index];
                        if (parameters != null && parameters.Length > 0)
                        {
                            objArray = parameters[index] as object[];
                        }
                        if (obj2 is TypeConverter)
                        {
                            TypeConverter typeConverter = obj2 as TypeConverter;
                            if (typeConverter.CanConvertFrom(type))
                            {
                                value = typeConverter.ConvertFrom((ITypeDescriptorContext)null, culture, value);
                            }
                        }
                        else if (obj2 is IValueConverter)
                        {
                            value = ((IValueConverter)obj2).Convert(value, targetType, (object)objArray, culture);
                        }
                        else if (obj2 is IChoValueConverter)
                        {
                            value = ((IChoValueConverter)obj2).Convert(value, targetType, (object)objArray, culture);
                        }
                    }
                    //if (value != obj1)
                    //    return value;
                }

                if (value == null)
                {
                    return(origType.Default());
                }
                if (targetType.IsAssignableFrom(value.GetType()) || targetType == value.GetType())
                {
                    return(value);
                }
                if (value is IConvertible)
                {
                    try
                    {
                        value = Convert.ChangeType(value, targetType, (IFormatProvider)culture);
                        if (obj1 != value)
                        {
                            return(value);
                        }
                    }
                    catch
                    {
                    }
                }
                if (ChoConvert.TryConvertXPlicit(value, targetType, "op_Explicit", ref value) ||
                    ChoConvert.TryConvertXPlicit(value, targetType, "op_Implicit", ref value))
                {
                    return(value);
                }

                object convValue = null;
                if (origType.IsNullableType())
                {
                    return(null);
                }
                else if (ChoConvert.TryConvertToSpecialValues(value, targetType, culture, out convValue))
                {
                    return(convValue);
                }

                if (value is Array && typeof(IList).IsAssignableFrom(targetType))
                {
                    if (typeof(Array).IsAssignableFrom(targetType))
                    {
                        MethodInfo convertMethod = typeof(ChoConvert).GetMethod("ConvertToArray",
                                                                                BindingFlags.NonPublic | BindingFlags.Static);
                        MethodInfo generic = convertMethod.MakeGenericMethod(new[] { targetType.GetItemType() });
                        return(generic.Invoke(null, new object[] { value }));
                    }
                    else
                    {
                        MethodInfo convertMethod = typeof(ChoConvert).GetMethod("ConvertToList",
                                                                                BindingFlags.NonPublic | BindingFlags.Static);
                        MethodInfo generic = convertMethod.MakeGenericMethod(new[] { targetType.GetItemType() });
                        return(generic.Invoke(null, new object[] { value }));
                    }
                }
                else if (value is IList && typeof(Array).IsAssignableFrom(targetType))
                {
                    if (typeof(Array).IsAssignableFrom(targetType))
                    {
                        MethodInfo convertMethod = typeof(ChoConvert).GetMethod("ConvertListToArray",
                                                                                BindingFlags.NonPublic | BindingFlags.Static);
                        MethodInfo generic = convertMethod.MakeGenericMethod(new[] { targetType.GetItemType() });
                        return(generic.Invoke(null, new object[] { value }));
                    }
                    else
                    {
                        MethodInfo convertMethod = typeof(ChoConvert).GetMethod("ConvertListToList",
                                                                                BindingFlags.NonPublic | BindingFlags.Static);
                        MethodInfo generic = convertMethod.MakeGenericMethod(new[] { targetType.GetItemType() });
                        return(generic.Invoke(null, new object[] { value }));
                    }
                }
                throw new ApplicationException("Object conversion failed.");
            }
            catch (Exception ex)
            {
                if (type.IsSimple())
                {
                    throw new ApplicationException(string.Format("Can't convert '{2}' value from '{0}' type to '{1}' type.", (object)type, (object)targetType, value), ex);
                }
                throw new ApplicationException(string.Format("Can't convert object from '{0}' type to '{1}' type.", (object)type, (object)targetType), ex);
            }
        }
示例#3
0
        public static object ConvertTo(object value, Type targetType, object sourceObject, object[] converters, object[] parameters, CultureInfo culture)
        {
            Type origType = targetType;

            targetType = targetType.IsNullableType() ? targetType.GetUnderlyingType() : targetType;
            object obj1 = value;

            if (targetType == (Type)null)
            {
                return(value);
            }
            //if (targetType == typeof(object))
            //    return value;
            if (culture == null)
            {
                culture = ChoConvert.DefaultCulture;
            }
            Type type = value == null ? typeof(object) : value.GetType().GetUnderlyingType();

            try
            {
                object[] objArray = (object[])null;
                if (converters.IsNullOrEmpty())
                {
                    converters = ChoTypeDescriptor.GetTypeConvertersForType(type);
                }

                if (converters != null && converters.Length > 0)
                {
                    for (int index = 0; index < converters.Length; ++index)
                    {
                        object obj2 = converters[index];
                        if (parameters != null && parameters.Length > 0)
                        {
                            objArray = parameters[index] as object[];
                        }
                        if (obj2 is TypeConverter)
                        {
                            TypeConverter typeConverter = obj2 as TypeConverter;
                            if (typeConverter.CanConvertTo(targetType))
                            {
                                value = typeConverter.ConvertTo((ITypeDescriptorContext)null, culture, value, targetType);
                            }
                        }
                        else if (obj2 is IValueConverter)
                        {
                            value = ((IValueConverter)obj2).ConvertBack(value, targetType, (object)objArray, culture);
                        }
                        else if (obj2 is IChoValueConverter)
                        {
                            value = ((IChoValueConverter)obj2).ConvertBack(value, targetType, (object)objArray, culture);
                        }
                    }
                    if (obj1 != value)
                    {
                        return(value);
                    }
                }
                if (value == null)
                {
                    return(origType.Default());
                }
                if (type == origType)
                {
                    return(value);
                }
                if (targetType.IsAssignableFrom(value.GetType()) || targetType == value.GetType())
                {
                    return(value);
                }
                if (value is IConvertible)
                {
                    try
                    {
                        value = Convert.ChangeType(value, targetType, (IFormatProvider)culture);
                        if (obj1 != value)
                        {
                            return(value);
                        }
                    }
                    catch
                    {
                    }
                }
                if (ChoConvert.TryConvertXPlicit(value, targetType, "op_Explicit", ref value) ||
                    ChoConvert.TryConvertXPlicit(value, targetType, "op_Implicit", ref value))
                {
                    //|| (!origType.IsNullableType() && ChoConvert.TryConvertToSpecialValues(value, targetType, culture, out value)))
                    //  || ChoConvert.TryConvertToSpecialValues(value, targetType, culture, out value))
                    return(value);
                }

                if (targetType == typeof(
                        ChoDynamicObject))
                {
                    dynamic ret = new ChoDynamicObject();
                    ret.Value = value;
                    return(ret);
                }

                object result = null;
                if (origType.IsNullableType())
                {
                    return(null);
                }
                else if (ChoConvert.TryConvertToSpecialValues(value, targetType, culture, out result))
                {
                    return(result);
                }

                throw new ApplicationException("Object conversion failed.");
            }
            catch (Exception ex)
            {
                if (type.IsSimple())
                {
                    throw new ApplicationException(string.Format("Can't convert '{2}' value from '{0}' type to '{1}' type.", (object)type, (object)targetType, value), ex);
                }
                throw new ApplicationException(string.Format("Can't convert object from '{0}' type to '{1}' type.", (object)type, (object)targetType), ex);
            }
        }
示例#4
0
        public static object ConvertFrom(object value, Type targetType, object sourceObject = null, object[] converters = null, object[] parameters = null, CultureInfo culture = null, string propName = null)
        {
            Type origType = targetType;

            targetType = targetType.IsNullableType() ? targetType.GetUnderlyingType() : targetType;
            if (targetType != null && targetType.IsValueType && origType != null & origType.IsNullableType())
            {
                if (value == null || (value is string && ((string)value).IsNullOrWhiteSpace()))
                {
                    return(null);
                }
            }

            object obj1 = value;

            if (targetType == (Type)null)
            {
                return(value);
            }
            //if (targetType == typeof(object))
            //    return value;
            if (culture == null)
            {
                culture = ChoConvert.DefaultCulture;
            }

            bool collectionConvertersFound = false;

            if (converters != null)
            {
                collectionConvertersFound = converters.OfType <IChoCollectionConverter>().Any();
            }

            if (targetType != typeof(object))
            {
                if (sourceObject is IChoConvertible && !propName.IsNullOrWhiteSpace())
                {
                    var    convObject    = sourceObject as IChoConvertible;
                    object convPropValue = null;
                    if (convObject.Convert(propName, value, culture, out convPropValue))
                    {
                        return(convPropValue);
                    }
                }

                if (!collectionConvertersFound)
                {
                    if (value is ICollection && !typeof(ICollection).IsAssignableFrom(targetType))
                    {
                        value = ((IEnumerable)value).FirstOrDefault <object>();
                    }
                    if (value != null && typeof(IList).IsAssignableFrom(targetType) && !(value is IList))
                    {
                        value = new object[] { value };
                    }
                }
            }

            Type type = value == null ? typeof(object) : value.GetType();

            try
            {
                if (converters.IsNullOrEmpty())
                {
                    converters = ChoTypeDescriptor.GetTypeConvertersForType(targetType);
                }

                if (converters != null && converters.Length > 0)
                {
                    object objArray = null;
                    for (int index = 0; index < converters.Length; ++index)
                    {
                        object conv = converters[index];
                        if (parameters != null && parameters.Length > 0)
                        {
                            objArray = parameters[index];
                        }

                        if (value is IList && !typeof(IChoCollectionConverter).IsAssignableFrom(conv.GetType()))
                        {
                            List <object> retValue = new List <object>();
                            object        lVal     = null;
                            foreach (var item in (IList)value)
                            {
                                if (conv is TypeConverter)
                                {
                                    TypeConverter typeConverter = conv as TypeConverter;
                                    if (typeConverter.CanConvertFrom(type))
                                    {
                                        lVal = typeConverter.ConvertFrom((ITypeDescriptorContext)null, culture, item);
                                    }
                                }
#if !NETSTANDARD2_0
                                else if (conv is IValueConverter)
                                {
                                    lVal = ((IValueConverter)conv).Convert(item, targetType, (object)objArray, culture);
                                }
#endif
                                else if (conv is IChoValueConverter)
                                {
                                    lVal = ((IChoValueConverter)conv).Convert(item, targetType, (object)objArray, culture);
                                }

                                retValue.Add(lVal);
                            }

                            value = retValue.ToArray();
                        }
                        else
                        {
                            if (conv is TypeConverter)
                            {
                                TypeConverter typeConverter = conv as TypeConverter;
                                if (typeConverter.CanConvertFrom(type))
                                {
                                    value = typeConverter.ConvertFrom((ITypeDescriptorContext)null, culture, value);
                                }
                            }
#if !NETSTANDARD2_0
                            else if (conv is IValueConverter)
                            {
                                value = ((IValueConverter)conv).Convert(value, targetType, (object)objArray, culture);
                            }
#endif
                            else if (conv is IChoValueConverter)
                            {
                                value = ((IChoValueConverter)conv).Convert(value, targetType, (object)objArray, culture);
                            }
                        }
                    }
                    //if (value != obj1)
                    //    return value;
                }

                if (targetType == typeof(object))
                {
                    return(value);
                }

                if (value == null)
                {
                    return(origType.Default());
                }
                if (targetType.IsAssignableFrom(value.GetType()) || targetType == value.GetType())
                {
                    return(value);
                }

                var srcType = value.GetType();
                var conv1   = TypeDescriptor.GetConverter(targetType);
                if (conv1 != null)
                {
                    try
                    {
                        if (conv1.CanConvertFrom(srcType))
                        {
                            return(conv1.ConvertFrom(value));
                        }
                    }
                    catch { }
                }

                if (value is IConvertible)
                {
                    try
                    {
                        value = Convert.ChangeType(value, targetType, (IFormatProvider)culture);
                        if (obj1 != value)
                        {
                            return(value);
                        }
                    }
                    catch
                    {
                    }
                }
                if (ChoConvert.TryConvertXPlicit(value, targetType, "op_Explicit", ref value) ||
                    ChoConvert.TryConvertXPlicit(value, targetType, "op_Implicit", ref value))
                {
                    return(value);
                }

                object convValue = null;
                if (origType.IsNullableType())
                {
                    return(null);
                }
                else if (ChoConvert.TryConvertToSpecialValues(value, targetType, culture, out convValue))
                {
                    return(convValue);
                }

                if (value is Array && typeof(IList).IsAssignableFrom(targetType))
                {
                    if (typeof(Array).IsAssignableFrom(targetType))
                    {
                        MethodInfo convertMethod = typeof(ChoConvert).GetMethod("ConvertToArray",
                                                                                BindingFlags.NonPublic | BindingFlags.Static);
                        MethodInfo generic = convertMethod.MakeGenericMethod(new[] { targetType.GetItemType() });
                        return(generic.Invoke(null, new object[] { value }));
                    }
                    else
                    {
                        MethodInfo convertMethod = typeof(ChoConvert).GetMethod("ConvertToList",
                                                                                BindingFlags.NonPublic | BindingFlags.Static);
                        MethodInfo generic = convertMethod.MakeGenericMethod(new[] { targetType.GetItemType() });
                        return(generic.Invoke(null, new object[] { value }));
                    }
                }
                else if (value is IList && typeof(Array).IsAssignableFrom(targetType))
                {
                    if (typeof(Array).IsAssignableFrom(targetType))
                    {
                        MethodInfo convertMethod = typeof(ChoConvert).GetMethod("ConvertListToArray",
                                                                                BindingFlags.NonPublic | BindingFlags.Static);
                        MethodInfo generic = convertMethod.MakeGenericMethod(new[] { targetType.GetItemType() });
                        return(generic.Invoke(null, new object[] { value }));
                    }
                    else
                    {
                        MethodInfo convertMethod = typeof(ChoConvert).GetMethod("ConvertListToList",
                                                                                BindingFlags.NonPublic | BindingFlags.Static);
                        MethodInfo generic = convertMethod.MakeGenericMethod(new[] { targetType.GetItemType() });
                        return(generic.Invoke(null, new object[] { value }));
                    }
                }
                throw new ApplicationException("Object conversion failed.");
            }
            catch (Exception ex)
            {
                if (type.IsSimple())
                {
                    throw new ApplicationException(string.Format("Can't convert '{2}' value from '{0}' type to '{1}' type.", (object)type, (object)targetType, value), ex);
                }
                throw new ApplicationException(string.Format("Can't convert object from '{0}' type to '{1}' type.", (object)type, (object)targetType), ex);
            }
        }
示例#5
0
        public static object ConvertTo(object value, Type targetType, object sourceObject, object[] converters, object[] parameters, CultureInfo culture,
                                       string propName = null)
        {
            Type origType = targetType;

            targetType = targetType.IsNullableType() ? targetType.GetUnderlyingType() : targetType;
            object obj1 = value;

            if (targetType == (Type)null)
            {
                return(value);
            }
            //if (targetType == typeof(object))
            //    return value;
            if (culture == null)
            {
                culture = ChoConvert.DefaultCulture;
            }
            Type type = value == null ? typeof(object) : value.GetType().GetUnderlyingType();

            try
            {
                if (sourceObject is IChoConvertible && !propName.IsNullOrWhiteSpace())
                {
                    var    convObject    = sourceObject as IChoConvertible;
                    object convPropValue = null;
                    if (convObject.ConvertBack(propName, value, targetType, culture, out convPropValue))
                    {
                        return(convPropValue);
                    }
                }

                object objArray = null;
                if (converters.IsNullOrEmpty())
                {
                    converters = ChoTypeDescriptor.GetTypeConvertersForType(type);
                }

                if (converters != null && converters.Length > 0)
                {
                    for (int index = 0; index < converters.Length; ++index)
                    {
                        object conv = converters[index];
                        if (parameters != null && parameters.Length > 0)
                        {
                            objArray = parameters[index];
                        }


                        if (value is IList && !typeof(IChoCollectionConverter).IsAssignableFrom(conv.GetType()))
                        {
                            List <object> retValue = new List <object>();
                            object        lVal     = null;
                            foreach (var item in (IList)value)
                            {
                                if (conv is TypeConverter)
                                {
                                    TypeConverter typeConverter = conv as TypeConverter;
                                    if (typeConverter.CanConvertFrom(type))
                                    {
                                        lVal = typeConverter.ConvertTo((ITypeDescriptorContext)null, culture, item, targetType.GetItemType());
                                    }
                                }
#if !NETSTANDARD2_0
                                else if (conv is IValueConverter)
                                {
                                    lVal = ((IValueConverter)conv).ConvertBack(item, targetType, (object)objArray, culture);
                                }
#endif
                                else if (conv is IChoValueConverter)
                                {
                                    lVal = ((IChoValueConverter)conv).ConvertBack(item, targetType, (object)objArray, culture);
                                }

                                retValue.Add(lVal);
                            }

                            value = retValue.ToArray();
                        }
                        else
                        {
                            if (conv is TypeConverter)
                            {
                                TypeConverter typeConverter = conv as TypeConverter;
                                if (typeConverter.CanConvertFrom(type))
                                {
                                    value = typeConverter.ConvertTo((ITypeDescriptorContext)null, culture, value, targetType);
                                }
                            }
#if !NETSTANDARD2_0
                            else if (conv is IValueConverter)
                            {
                                value = ((IValueConverter)conv).ConvertBack(value, targetType, (object)objArray, culture);
                            }
#endif
                            else if (conv is IChoValueConverter)
                            {
                                value = ((IChoValueConverter)conv).ConvertBack(value, targetType, (object)objArray, culture);
                            }
                        }
                    }
                    if (obj1 != value)
                    {
                        return(value);
                    }
                }
                if (value == null)
                {
                    return(origType.Default());
                }
                if (type == origType)
                {
                    return(value);
                }
                if (targetType.IsAssignableFrom(value.GetType()) || targetType == value.GetType())
                {
                    return(value);
                }

                var srcType = value.GetType();
                var conv1   = TypeDescriptor.GetConverter(targetType);
                if (conv1 != null)
                {
                    try
                    {
                        if (conv1.CanConvertFrom(srcType))
                        {
                            return(conv1.ConvertFrom(value));
                        }
                    }
                    catch { }
                }

                if (value is IConvertible)
                {
                    try
                    {
                        value = Convert.ChangeType(value, targetType, (IFormatProvider)culture);
                        if (obj1 != value)
                        {
                            return(value);
                        }
                    }
                    catch
                    {
                    }
                }
                if (ChoConvert.TryConvertXPlicit(value, targetType, "op_Explicit", ref value) ||
                    ChoConvert.TryConvertXPlicit(value, targetType, "op_Implicit", ref value))
                {
                    //|| (!origType.IsNullableType() && ChoConvert.TryConvertToSpecialValues(value, targetType, culture, out value)))
                    //  || ChoConvert.TryConvertToSpecialValues(value, targetType, culture, out value))
                    return(value);
                }

                if (targetType == typeof(
                        ChoDynamicObject))
                {
                    dynamic ret = new ChoDynamicObject();
                    ret.Value = value;
                    return(ret);
                }

                object result = null;
                if (origType.IsNullableType())
                {
                    return(null);
                }
                else if (ChoConvert.TryConvertToSpecialValues(value, targetType, culture, out result))
                {
                    return(result);
                }

                throw new ApplicationException("Object conversion failed.");
            }
            catch (Exception ex)
            {
                if (type.IsSimple())
                {
                    throw new ApplicationException(string.Format("Can't convert '{2}' value from '{0}' type to '{1}' type.", (object)type, (object)targetType, value), ex);
                }
                throw new ApplicationException(string.Format("Can't convert object from '{0}' type to '{1}' type.", (object)type, (object)targetType), ex);
            }
        }
示例#6
0
        public static void ConvertNSetMemberValue(this IDictionary <string, object> dict, string fn, ChoRecordFieldConfiguration fieldConfig, ref object fieldValue, CultureInfo culture)
        {
            if (fieldConfig.Converters.IsNullOrEmpty())
            {
                fieldValue = ChoConvert.ConvertFrom(fieldValue, fieldConfig.FieldType, null, ChoTypeDescriptor.GetTypeConvertersForType(fieldConfig.FieldType), null, culture);
            }
            else
            {
                fieldValue = ChoConvert.ConvertFrom(fieldValue, fieldConfig.FieldType, null, fieldConfig.Converters.ToArray(), null, culture);
            }

            dict.AddOrUpdate(fn, fieldValue);
        }
示例#7
0
        public static bool GetFallbackValue(this IDictionary <string, object> dict, string fn, ChoRecordFieldConfiguration fieldConfig, CultureInfo culture, ref object fieldValue)
        {
            //Set Fallback value to member
            if (fieldConfig.IsFallbackValueSpecified)
            {
                if (fieldConfig.Converters.IsNullOrEmpty())
                {
                    fieldValue = ChoConvert.ConvertFrom(fieldConfig.FallbackValue, fieldConfig.FieldType, null, ChoTypeDescriptor.GetTypeConvertersForType(fieldConfig.FieldType), null, culture);
                }
                else
                {
                    fieldValue = ChoConvert.ConvertFrom(fieldConfig.FallbackValue, fieldConfig.FieldType, null, fieldConfig.Converters.ToArray(), null, culture);
                }
            }

            return(fieldConfig.IsFallbackValueSpecified);
        }
示例#8
0
        public static void SetDefaultValue(this IDictionary <string, object> dict, string fn, ChoRecordFieldConfiguration fieldConfig, CultureInfo culture)
        {
            object fieldValue = null;

            //Set default value to member
            try
            {
                if (fieldConfig.IsDefaultValueSpecified)
                {
                    if (fieldConfig.Converters.IsNullOrEmpty())
                    {
                        fieldValue = ChoConvert.ConvertFrom(fieldConfig.DefaultValue, fieldConfig.FieldType, null, ChoTypeDescriptor.GetTypeConvertersForType(fieldConfig.FieldType), null, culture);
                    }
                    else
                    {
                        fieldValue = ChoConvert.ConvertFrom(fieldConfig.DefaultValue, fieldConfig.FieldType, null, fieldConfig.Converters.ToArray(), null, culture);
                    }

                    dict.AddOrUpdate(fn, fieldValue);
                }
            }
            catch { }
        }