Exemplo n.º 1
0
        public static object ChangeValueType(object from, Type toType)
        {
            var fromType = from.GetType();

            if (!fromType.IsEnum && !toType.IsEnum)
            {
                if (toType == typeof(char) && from is string s)
                {
                    return(s.Length > 0 ? (object)s[0] : null);
                }
                if (toType == typeof(string) && from is char c)
                {
                    return(c.ToString());
                }
                if (toType == typeof(TimeSpan) && from is long ticks)
                {
                    return(new TimeSpan(ticks));
                }
                if (toType == typeof(long) && from is TimeSpan time)
                {
                    return(time.Ticks);
                }

                var destNumberType = DynamicNumber.GetNumber(toType);
                var value          = destNumberType?.ConvertFrom(from);
                if (value != null)
                {
                    if (toType == typeof(char))
                    {
                        return(value.ToString()[0]);
                    }

                    return(value);
                }

                if (toType == typeof(string))
                {
                    var srcNumberType = DynamicNumber.GetNumber(from.GetType());
                    if (srcNumberType != null)
                    {
                        return(srcNumberType.ToString(@from));
                    }
                }
            }

            if (from is string strValue)
            {
                return(TypeSerializer.DeserializeFromString(strValue, toType));
            }

            if (toType == typeof(string))
            {
                return(from.ToJsv());
            }

            if (toType.HasInterface(typeof(IConvertible)))
            {
                return(Convert.ChangeType(from, toType, provider: null));
            }

            return(TypeSerializer.DeserializeFromString(from.ToJsv(), toType));
        }
Exemplo n.º 2
0
        public static T ConvertTo <T>(this object from)
        {
            if (from == null)
            {
                return(default(T));
            }

            var fromType = from.GetType();

            if (fromType == typeof(T))
            {
                return((T)from);
            }

            if (fromType.IsValueType || typeof(T).IsValueType)
            {
                if (!fromType.IsEnum && !typeof(T).IsEnum)
                {
                    if (typeof(T) == typeof(char) && from is string s)
                    {
                        return((T)(s.Length > 0 ? (object)s[0] : null));
                    }
                    if (typeof(T) == typeof(string) && from is char c)
                    {
                        return((T)(object)c.ToString());
                    }

                    var destNumberType = DynamicNumber.GetNumber(typeof(T));
                    var value          = destNumberType?.ConvertFrom(from);
                    if (value != null)
                    {
                        if (typeof(T) == typeof(char))
                        {
                            return((T)(object)value.ToString()[0]);
                        }

                        return((T)value);
                    }

                    if (typeof(T) == typeof(string))
                    {
                        var srcNumberType = DynamicNumber.GetNumber(from.GetType());
                        if (srcNumberType != null)
                        {
                            return((T)(object)srcNumberType.ToString(from));
                        }
                    }
                }

                return((T)ChangeValueType(from, typeof(T)));
            }

            if (typeof(IEnumerable).IsAssignableFrom(typeof(T)))
            {
                var listResult = TranslateListWithElements.TryTranslateCollections(
                    fromType, typeof(T), from);

                return((T)listResult);
            }

            var to = typeof(T).CreateInstance <T>();

            return(to.PopulateWith(from));
        }