Exemplo n.º 1
0
        private static bool TryFromString(
            string value,
            XInfo from,
            XInfo to,
            out object result,
            string format)
        {
            result = GetDefaultValue(to.Category);

            decimal decimalVal;
            var     decimalFlag = decimal.TryParse(value, out decimalVal);

            switch (to.Category)
            {
            case TypeCategory.Array:
                if (to.IsBytes)
                {
                    result = IsToConfig.Encoding.GetBytes(value);
                    return(true);
                }
                var firstElement = to
                                   .ElementInfos
                                   .FirstOrDefault();
                if (
                    null != firstElement
                    &&
                    firstElement.Category
                    ==
                    TypeCategory.Class)
                {
                    if (!IsJson(value))
                    {
                        return(false);
                    }
                    result = JsonConvert.DeserializeObject(
                        value,
                        to.Type
                        );
                    return(true);
                }
                return(TryToArray(
                           from,
                           to,
                           value,
                           out result,
                           format
                           ));

            case TypeCategory.String:
                result = value;
                return(true);

            case TypeCategory.Stream:
                result = new MemoryStream(value.To <byte[]>());
                return(true);

            case TypeCategory.Enum:
                result = IsToConverter.FromString.ToEnum(
                    to.Type,
                    value
                    );
                return(true);

            case TypeCategory.Class:
            case TypeCategory.Struct:
                if (IsJson(value))
                {
                    result = JsonConvert.DeserializeObject(
                        value,
                        to.Type
                        );
                    return(true);
                }
                return(false);

            case TypeCategory.DateTime:
                DateTime val;
                if (DateTime.TryParse(value, out val))
                {
                    result = val;
                    return(true);
                }
                else
                {
                    int[] points;
                    if (TryParseDate(value, out points))
                    {
                        try {
                            var millisecond = points[6]
                                              .To <string>()
                                              .Left(3)
                                              .To <int>();
                            result = new DateTime(
                                ToExtender.Enclosed(points[0], 1, 9999),
                                ToExtender.Enclosed(points[1], 1, 12),
                                ToExtender.Enclosed(points[2], 1, 31),
                                ToExtender.Enclosed(points[3], 0, 23),
                                ToExtender.Enclosed(points[4], 0, 59),
                                ToExtender.Enclosed(points[5], 0, 59),
                                millisecond
                                );
                        } catch (Exception ex) {
                            LogRecord
                            .Create()
                            //.Add("Value", value)
                            //.Add("From", from.FullName)
                            //.Add("To", to.FullName)
                            //.Add(
                            //	"Points",
                            //	points
                            //		.Select(x => x.ToString())
                            //		.Join(Symbol.CommaSpace)
                            //)
                            //.Add(ex)
                            .Error();
                            return(false);
                        }
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }

            case TypeCategory.Boolean:
                if (IsNumeric(value))
                {
                    var i = value.To <int>();
                    result = i > 0;
                    return(true);
                }

                foreach (var one in IsToConfig.TrueStringArray)
                {
                    if (one.Equals(
                            value,
                            StringComparison.OrdinalIgnoreCase))
                    {
                        result = true;
                        break;
                    }
                }
                return(true);

            case TypeCategory.Char:
                result = Convert.ToChar(value);
                return(true);

            case TypeCategory.Decimal:
                result = decimalVal;
                return(decimalFlag);

            case TypeCategory.Byte:
                if (!decimalFlag)
                {
                    return(false);
                }
                byte byteVal;
                if (decimalVal.TryTo <byte>(out byteVal))
                {
                    result = byteVal;
                    return(true);
                }
                else
                {
                    return(false);
                }

            case TypeCategory.SByte:
                if (!decimalFlag)
                {
                    return(false);
                }
                sbyte sbyteVal;
                if (decimalVal.TryTo <sbyte>(out sbyteVal))
                {
                    result = sbyteVal;
                    return(true);
                }
                else
                {
                    return(false);
                }

            case TypeCategory.Int16:
                if (!decimalFlag)
                {
                    return(false);
                }
                Int16 int16Val;
                if (decimalVal.TryTo <Int16>(out int16Val))
                {
                    result = int16Val;
                    return(true);
                }
                else
                {
                    return(false);
                }

            case TypeCategory.UInt16:
                if (!decimalFlag)
                {
                    return(false);
                }
                UInt16 uint16Val;
                if (decimalVal.TryTo <UInt16>(out uint16Val))
                {
                    result = uint16Val;
                    return(true);
                }
                else
                {
                    return(false);
                }

            case TypeCategory.Int32:
                if (!decimalFlag)
                {
                    return(false);
                }
                Int32 int32Val;
                if (decimalVal.TryTo <Int32>(out int32Val))
                {
                    result = int32Val;
                    return(true);
                }
                else
                {
                    return(false);
                }

            case TypeCategory.UInt32:
                if (!decimalFlag)
                {
                    return(false);
                }
                UInt32 uint32Val;
                if (decimalVal.TryTo <UInt32>(out uint32Val))
                {
                    result = uint32Val;
                    return(true);
                }
                else
                {
                    return(false);
                }

            case TypeCategory.Int64:
                if (!decimalFlag)
                {
                    return(false);
                }
                Int64 int64Val;
                if (decimalVal.TryTo <Int64>(out int64Val))
                {
                    result = int64Val;
                    return(true);
                }
                else
                {
                    return(false);
                }

            case TypeCategory.UInt64:
                if (!decimalFlag)
                {
                    return(false);
                }
                UInt64 uint64Val;
                if (decimalVal.TryTo <UInt64>(out uint64Val))
                {
                    result = uint64Val;
                    return(true);
                }
                else
                {
                    return(false);
                }

            case TypeCategory.Single:
                if (!decimalFlag)
                {
                    return(false);
                }
                Single singleVal;
                if (decimalVal.TryTo <Single>(out singleVal))
                {
                    result = singleVal;
                    return(true);
                }
                else
                {
                    return(false);
                }

            case TypeCategory.Double:
                if (!decimalFlag)
                {
                    return(false);
                }
                Double doubleVal;
                if (decimalVal.TryTo <Double>(out doubleVal))
                {
                    result = doubleVal;
                    return(true);
                }
                else
                {
                    return(false);
                }

            case TypeCategory.Interface:
            case TypeCategory.IntPtr:
            case TypeCategory.UIntPtr:
            case TypeCategory.Null:
            case TypeCategory.Others:
            default:
                return(false);
            }
        }
Exemplo n.º 2
0
        private static bool TryToArray(
            XInfo from,
            XInfo to,
            object value,
            out object result,
            string format = "")
        {
            result = null;

            try {
                var elementType = to
                                  .ElementInfos
                                  .FirstOrDefault()
                                  .Type;
                var genericListTypeRunTime = typeof(List <>)
                                             .MakeGenericType(new[] { elementType });
                var genericList = Activator.CreateInstance(
                    genericListTypeRunTime
                    );
                var genericListType = genericList.GetType();

                // Add
                var addInfo = genericListType
                              .GetMethods()
                              .FirstOrDefault(x => x.Name == "Add");
                if (from.Category == TypeCategory.Array)
                {
                    if (to.ElementInfos.Count() == 1)
                    {
                        var datas = value as IEnumerable;
                        if (null != datas)
                        {
                            var en = datas.GetEnumerator();
                            while (en.MoveNext())
                            {
                                object val;
                                if (TryTo(
                                        en.Current,
                                        elementType,
                                        out val))
                                {
                                    addInfo.Invoke(
                                        genericList,
                                        new object[] { val }
                                        );
                                }
                                else
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                    else
                    {
                        //
                    }
                }
                else
                {
                    if (to.ElementInfos.Count() == 1)
                    {
                        object val;

                        if (value.Is(elementType))
                        {
                            addInfo.Invoke(
                                genericList,
                                new object[] { value }
                                );
                        }
                        else
                        {
                            if (TryTo(value, elementType, out val))
                            {
                                addInfo.Invoke(
                                    genericList,
                                    new object[] { val }
                                    );
                            }
                            else
                            {
                                return(false);
                            }
                        }
                    }
                    else
                    {
                        //
                    }
                }

                if (to.Type.FullName == genericListType.FullName)
                {
                    // List
                    result = genericList;
                }
                else if (to.Type.Is <IQueryable>())
                {
                    // IQueryable
                    result = ((IList)genericList).AsQueryable();
                }
                else if (
                    to.Category == TypeCategory.Interface
                    &&
                    to.Type.Is <IList>())
                {
                    // IList
                    result = (IList)genericList;
                }
                else
                {
                    // Array
                    var toarrayInfo = genericListType
                                      .GetMethods()
                                      .FirstOrDefault(x => x.Name == "ToArray");
                    if (null == toarrayInfo)
                    {
                        return(false);
                    }
                    result = toarrayInfo.Invoke(
                        genericList,
                        new object[] { }
                        );
                }

                return(true);
            } catch (Exception ex) {
                LogRecord
                .Create()
                //.Add("From", from?.Type.Name)
                //.Add("To", to?.Type.Name)
                //.Add("ValueType", value?.GetType().Name)
                //.Add("Value", value)
                //.Add(ex)
                .Error();
                return(false);
            }
        }
Exemplo n.º 3
0
        private static bool TryFromClass(
            object value,
            XInfo from,
            XInfo to,
            out object result,
            string format)
        {
            result = GetDefaultValue(to.Category);

            switch (to.Category)
            {
            case TypeCategory.Array:
                return(TryToArray(
                           from,
                           to,
                           value,
                           out result,
                           format
                           ));

            case TypeCategory.Class:
                if (from.Equals(to))
                {
                    result = value;
                    return(true);
                }

                var underlyingType = Nullable.GetUnderlyingType(
                    to.Type
                    );
                var converter = underlyingType == null
                                                ? TypeDescriptor.GetConverter(to.Type)
                                                : TypeDescriptor.GetConverter(underlyingType);

                if (null != converter)
                {
                    try {
                        result = converter.ConvertFrom(value);
                        return(true);
                    } catch (Exception ex) {
                        LogRecord
                        .Create()
                        //.Add("Typeconverter", converter.GetType().Name)
                        //.Add("From", from?.Type.Name)
                        //.Add("To", to?.Type.Name)
                        //.Add("ValueType", value?.GetType().Name)
                        //.Add("Value", value)
                        //.Add(ex)
                        .Error();
                    }
                }
                result = ForceClone(value, to.Type);
                return(true);

            case TypeCategory.Struct:
                var dic = GetValues(value);
                result = SetValues(dic, to.Type);
                return(true);

            case TypeCategory.Enum:
            case TypeCategory.Interface:
            case TypeCategory.Stream:
            case TypeCategory.Color:
            case TypeCategory.String:
            case TypeCategory.DateTime:
            case TypeCategory.Decimal:
            case TypeCategory.Boolean:
            case TypeCategory.Char:
            case TypeCategory.Byte:
            case TypeCategory.SByte:
            case TypeCategory.Int16:
            case TypeCategory.UInt16:
            case TypeCategory.Int32:
            case TypeCategory.UInt32:
            case TypeCategory.IntPtr:
            case TypeCategory.UIntPtr:
            case TypeCategory.Int64:
            case TypeCategory.UInt64:
            case TypeCategory.Single:
            case TypeCategory.Double:
            case TypeCategory.Null:
            case TypeCategory.Others:
            default:
                return(false);
            }
        }
Exemplo n.º 4
0
        private static bool TryTo(
            object value,
            Type type,
            out object result,
            string format = "")
        {
            result = string.Empty;
            if (null == value || null == type)
            {
                return(false);
            }

            var from = ToCache.Get(value);

            var interceptor = ToInterceptor.Get(from.Type);

            if (null != interceptor)
            {
                return(interceptor.TryTo(
                           value,
                           type,
                           out result,
                           format
                           ));
            }

            var to = ToCache.Get(type);

            try {
                switch (from.Category)
                {
                case TypeCategory.Array:
                    object arry = value as Array;
                    if (null == arry)
                    {
                        var toarrayInfo = from
                                          .Type
                                          .GetMethods()
                                          .FirstOrDefault(x =>
                                                          x.Name == "ToArray"
                                                          );
                        if (null == toarrayInfo)
                        {
                            return(false);
                        }
                        arry = toarrayInfo.Invoke(
                            value,
                            new object[] { }
                            );
                    }
                    if (null == arry)
                    {
                        return(false);
                    }
                    return(TryFromArray(
                               arry as Array,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.Enum:
                    return(TryFromEnum(
                               value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.Interface:
                    return(TryFromInterface(
                               value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.Class:
                    return(TryFromClass(
                               value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.Stream:
                    return(TryFromStream(
                               value as Stream,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.Color:
                //return TryFromColor(
                //	(Color)value,
                //	from,
                //	to,
                //	out result,
                //	format
                //);
                case TypeCategory.String:
                    return(TryFromString(
                               value as string,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.DateTime:
                    return(TryFromDateTime(
                               (DateTime)value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.Decimal:
                    return(TryFromDecimal(
                               (decimal)value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.Boolean:
                    return(TryFromBoolean(
                               (bool)value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.Char:
                    return(TryFromChar(
                               (char)value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.Byte:
                    return(TryFromByte(
                               (byte)value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.SByte:
                    return(TryFromSByte(
                               (sbyte)value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.Int16:
                    return(TryFromInt16(
                               (short)value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.UInt16:
                    return(TryFromUInt16(
                               (ushort)value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.Int32:
                    return(TryFromInt32(
                               (int)value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.UInt32:
                    return(TryFromUInt32(
                               (uint)value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.IntPtr:
                    return(TryFromIntPtr(
                               (IntPtr)value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.UIntPtr:
                    return(TryFromUIntPtr(
                               (UIntPtr)value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.Int64:
                    return(TryFromInt64(
                               (long)value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.UInt64:
                    return(TryFromUInt64(
                               (ulong)value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.Single:
                    return(TryFromSingle(
                               (float)value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.Double:
                    return(TryFromDouble(
                               (double)value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.Struct:
                    return(TryFromStruct(
                               value,
                               from,
                               to,
                               out result,
                               format
                               ));

                case TypeCategory.Null:
                    return(false);

                case TypeCategory.Others:
                    return(false);

                default:
                    return(false);
                }
            } catch (Exception ex) {
                LogRecord
                .Create()
                //.Add("Value", value?.ToString())
                //.Add("Type", type?.GetType().Name)
                //.Add("Result", result)
                //.Add("Format", format)
                //.Add(ex)
                .Error();
                return(false);
            }
        }