示例#1
0
        private static bool TryFromUIntPtr(
            UIntPtr 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.UIntPtr:
                result = value;
                return(true);

            case TypeCategory.Enum:
            case TypeCategory.Interface:
            case TypeCategory.Class:
            case TypeCategory.Struct:
            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.Int64:
            case TypeCategory.UInt64:
            case TypeCategory.Single:
            case TypeCategory.Double:
            case TypeCategory.Null:
            case TypeCategory.Others:
            default:
                return(false);
            }
        }
示例#2
0
文件: ToCache.cs 项目: ChenKa1/IsTo
        internal static XInfo Get(object value)
        {
            if (null == value)
            {
                throw new ArgumentNullException("value");
            }

            var type = value is Type
                                ? (Type)value
                                : value.GetType();

            XInfo info;

            if (!_Cache.TryGetValue(type, out info))
            {
                info = new XInfo(type);
                _Cache.TryAdd(type, info);
            }
            return(info);
        }
示例#3
0
        private static bool TryFromUInt16(
            ushort value,
            XInfo from,
            XInfo to,
            out object result,
            string format)
        {
            result = GetDefaultValue(to.Category);

            switch (to.Category)
            {
            case TypeCategory.Array:
                if (to.IsBytes)
                {
                    result = BitConverter.GetBytes(value);
                    return(true);
                }
                return(TryToArray(
                           from,
                           to,
                           value,
                           out result,
                           format
                           ));

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

            case TypeCategory.Stream:
                var bytes = BitConverter.GetBytes(value);
                result = new MemoryStream(bytes);
                return(true);

            case TypeCategory.Enum:
            case TypeCategory.String:
                var s = value.ToString();
                return(TryFromString(
                           s,
                           XInfo.String.Value,
                           to,
                           out result,
                           format
                           ));

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

            case TypeCategory.Boolean:
                result = value > 0;
                return(true);

            case TypeCategory.Color:
                var i = (Int32)value;
                return(TryFromInt32(
                           i,
                           XInfo.Int32.Value,
                           to,
                           out result,
                           format
                           ));

            case TypeCategory.Decimal:
                result = (Decimal)value;
                return(NumericCompare(value, result));

            case TypeCategory.Byte:
                checked { result = (Byte)value; }
                return(NumericCompare(value, result));

            case TypeCategory.SByte:
                checked { result = (SByte)value; }
                return(NumericCompare(value, result));

            case TypeCategory.Int16:
                checked { result = (Int16)value; }
                return(NumericCompare(value, result));

            case TypeCategory.Int32:
                result = (Int32)value;
                return(NumericCompare(value, result));

            case TypeCategory.UInt32:
                result = (UInt32)value;
                return(NumericCompare(value, result));

            case TypeCategory.Int64:
                result = (Int64)value;
                return(NumericCompare(value, result));

            case TypeCategory.UInt64:
                result = (UInt64)value;
                return(NumericCompare(value, result));

            case TypeCategory.Single:
                result = (Single)value;
                return(NumericCompare(value, result));

            case TypeCategory.Double:
                result = (Double)value;
                return(NumericCompare(value, result));

            case TypeCategory.Interface:
            case TypeCategory.Class:
            case TypeCategory.Struct:
            case TypeCategory.DateTime:
            case TypeCategory.IntPtr:
            case TypeCategory.UIntPtr:
            case TypeCategory.Null:
            case TypeCategory.Others:
            default:
                return(false);
            }
        }
示例#4
0
        private static bool TryFromStruct(
            object value,
            XInfo from,
            XInfo to,
            out object result,
            string format)
        {
            result = GetDefaultValue(to.Category);

            var str = value.ToString();

            if (from.Equals(to))
            {
                result = value;
                return(true);
            }

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

            case TypeCategory.Array:
            case TypeCategory.String:
            case TypeCategory.Stream:
            case TypeCategory.Color:
            case TypeCategory.Enum:
            case TypeCategory.DateTime:
            case TypeCategory.Boolean:
            case TypeCategory.Char:
            case TypeCategory.Decimal:
            case TypeCategory.Byte:
            case TypeCategory.SByte:
            case TypeCategory.Int16:
            case TypeCategory.UInt16:
            case TypeCategory.Int32:
            case TypeCategory.UInt32:
            case TypeCategory.Int64:
            case TypeCategory.UInt64:
            case TypeCategory.Single:
            case TypeCategory.Double:
                return(TryFromString(
                           str,
                           from,
                           to,
                           out result,
                           format
                           ));

            case TypeCategory.Interface:
            case TypeCategory.IntPtr:
            case TypeCategory.UIntPtr:
            case TypeCategory.Null:
            case TypeCategory.Others:
            default:
                return(false);
            }
        }
示例#5
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) {
                return(false);
            }
        }
示例#6
0
        private static bool TryFromByte(
            byte 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.Byte:
                result = value;
                return(true);

            case TypeCategory.Stream:
                var bytes = BitConverter.GetBytes(value);
                result = new MemoryStream(bytes);
                return(true);

            case TypeCategory.String:
                var input = UnicodeEncoding.UTF8.GetString(
                    new[] { value }
                    );
                return(TryFromString(
                           input,
                           XInfo.String.Value,
                           to,
                           out result,
                           format
                           ));

            case TypeCategory.Enum:
                result = Enum.Parse(to.Type, value.To <string>());
                return(true);

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

            case TypeCategory.Boolean:
                result = value > 0;
                return(true);

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

            case TypeCategory.SByte:
                checked { result = (SByte)value; }
                return(true);

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

            case TypeCategory.UInt16:
                checked { result = (UInt16)value; }
                return(true);

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

            case TypeCategory.UInt32:
                checked { result = (UInt32)value; }
                return(true);

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

            case TypeCategory.UInt64:
                checked { result = (UInt64)value; }
                return(true);

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

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

            case TypeCategory.Color:
                result = Color.FromArgb((Int32)value);
                return(true);

            case TypeCategory.Interface:
            case TypeCategory.Class:
            case TypeCategory.Struct:
            case TypeCategory.DateTime:
            case TypeCategory.IntPtr:
            case TypeCategory.UIntPtr:
            case TypeCategory.Null:
            case TypeCategory.Others:
            default:
                return(false);
            }
        }
示例#7
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 = UnicodeEncoding.UTF8.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.Color:
                // TODO: HSL, HSLA

                if (IsHtmlColor(value))
                {
                    result = ColorTranslator.FromHtml(value);
                    return(true);
                }

                if (IsNumeric(value))
                {
                    var argb = value.To <int>();
                    result = Color.FromArgb(argb);
                    return(true);
                }

                int r, g, b, a;
                if (IsRGBColor(value, out r, out g, out b))
                {
                    result = Color.FromArgb(r, g, b);
                    return(true);
                }
                if (IsARGBColor(value, out a, out r, out g, out b))
                {
                    result = Color.FromArgb(a, r, g, b);
                    return(true);
                }

                KnownColor nc;
                if (value.TryTo <KnownColor>(out nc))
                {
                    result = Color.FromName(value);
                    return(true);
                }
                else
                {
                    return(false);
                }

            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))
                    {
                        result = new DateTime(
                            points[0],
                            points[1],
                            points[2],
                            points[3],
                            points[4],
                            points[5],
                            points[6]
                            );
                        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);
            }
        }
示例#8
0
        private static bool TryFromColor(
            Color value,
            XInfo from,
            XInfo to,
            out object result,
            string format)
        {
            result = GetDefaultValue(to.Category);

            var i = value.ToArgb();
            var n = value.Name;


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

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

            case TypeCategory.Enum:
                try {
                    result = Enum.Parse(to.Type, n);
                    return(true);
                } catch { }
                try {
                    result = Enum.Parse(to.Type, i.ToString());
                    return(true);
                } catch { }
                return(false);

            case TypeCategory.Stream:
            case TypeCategory.String:
                var f = string.IsNullOrEmpty(format)
                                                ? "html" : format.ToLower();
                var s = string.Empty;
                switch (f)
                {
                case "html":
                    s = ColorTranslator.ToHtml(value);
                    break;

                case "rgb":
                    s = string.Format(
                        "rgb({0}, {1}, {2})",
                        value.R,
                        value.G,
                        value.B
                        );
                    break;

                case "argb":
                    s = string.Format(
                        "rgba({0}, {1}, {2}, {3}%)",
                        value.R,
                        value.G,
                        value.B,
                        value.A
                        );
                    break;

                case "name":
                    s = value.Name;
                    break;

                default:
                    return(false);
                }

                return(TryFromString(
                           s,
                           XInfo.String.Value,
                           to,
                           out result,
                           format
                           ));

            case TypeCategory.Decimal:
                result = (Decimal)i;
                return(NumericCompare(i, result));

            case TypeCategory.Byte:
                checked { result = (Byte)i; }
                return(NumericCompare(i, result));

            case TypeCategory.SByte:
                checked { result = (SByte)i; }
                return(NumericCompare(i, result));

            case TypeCategory.Int16:
                checked { result = (Int16)i; }
                return(NumericCompare(i, result));

            case TypeCategory.UInt16:
                checked { result = (UInt16)i; }
                return(NumericCompare(i, result));

            case TypeCategory.Int32:
                result = (Int32)i;
                return(NumericCompare(i, result));

            case TypeCategory.UInt32:
                checked { result = (UInt32)i; }
                return(NumericCompare(i, result));

            case TypeCategory.Int64:
                result = (Int64)i;
                return(NumericCompare(i, result));

            case TypeCategory.UInt64:
                result = (UInt64)i;
                return(NumericCompare(i, result));

            case TypeCategory.Single:
                result = (Single)i;
                return(NumericCompare(i, result));

            case TypeCategory.Double:
                result = (Double)i;
                return(NumericCompare(i, result));

            case TypeCategory.DateTime:
            case TypeCategory.Boolean:
            case TypeCategory.Char:
            case TypeCategory.Class:
            case TypeCategory.Struct:
            case TypeCategory.Interface:
            case TypeCategory.IntPtr:
            case TypeCategory.UIntPtr:
            case TypeCategory.Null:
            case TypeCategory.Others:
            default:
                return(false);
            }
        }
示例#9
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) {
                        //
                    }
                }
                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);
            }
        }
示例#10
0
        private static bool TryFromArray(
            Array value,
            XInfo from,
            XInfo to,
            out object result,
            string format)
        {
            result = GetDefaultValue(to.Category);

            var bytes = from.IsBytes
                                ? value.Cast <byte>().ToArray()
                                : new byte[0];

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

            case TypeCategory.DateTime:
                if (from.IsBytes)
                {
                    if (bytes.Length == 8)
                    {
                        result = bytes.To <Int64>().To <DateTime>();
                        return(true);
                    }
                    else
                    {
                        var s = UnicodeEncoding.UTF8.GetString(bytes);
                        return(TryFromString(
                                   s,
                                   XInfo.String.Value,
                                   to,
                                   out result,
                                   format
                                   ));
                    }
                }
                return(false);

            case TypeCategory.Enum:
            case TypeCategory.Class:
            case TypeCategory.Struct:
            case TypeCategory.Stream:
            case TypeCategory.Color:
            case TypeCategory.String:
                if (from.IsBytes)
                {
                    var str = UnicodeEncoding.UTF8.GetString(bytes);
                    return(TryFromString(
                               str,
                               XInfo.String.Value,
                               to,
                               out result,
                               format
                               ));
                }
                return(false);

            case TypeCategory.Byte:
                if (from.IsBytes)
                {
                    if (bytes.Length == 0)
                    {
                        result = bytes[0];
                        return(true);
                    }
                }
                return(false);

            case TypeCategory.SByte:
                if (from.IsBytes)
                {
                    if (bytes.Length == 0)
                    {
                        result = (SByte)bytes[0];
                        return(NumericCompare(value, result));
                    }
                }
                return(false);

            case TypeCategory.Decimal:
                if (bytes.Length != 16)
                {
                    // A decimal must be created from exactly
                    // 128 bits / 16 bytes
                    return(false);
                }
                var bits = new Int32[4];
                for (int i = 0; i <= 15; i += 4)
                {
                    bits[i / 4] = BitConverter.ToInt32(bytes, i);
                }
                result = new decimal(bits);
                return(true);

            case TypeCategory.Boolean:
                if (from.IsBytes)
                {
                    result = BitConverter.ToBoolean(bytes, 0);
                    return(true);
                }
                return(false);

            case TypeCategory.Char:
                if (from.IsBytes)
                {
                    result = BitConverter.ToChar(bytes, 0);
                    return(true);
                }
                return(false);

            case TypeCategory.Int16:
                if (from.IsBytes)
                {
                    result = BitConverter.ToInt16(bytes, 0);
                    return(true);
                }
                return(false);

            case TypeCategory.UInt16:
                if (from.IsBytes)
                {
                    result = BitConverter.ToUInt16(bytes, 0);
                    return(true);
                }
                return(false);

            case TypeCategory.Int32:
                if (from.IsBytes)
                {
                    result = BitConverter.ToInt32(bytes, 0);
                    return(true);
                }
                return(false);

            case TypeCategory.UInt32:
                if (from.IsBytes)
                {
                    result = BitConverter.ToUInt32(bytes, 0);
                    return(true);
                }
                return(false);

            case TypeCategory.Int64:
                if (from.IsBytes)
                {
                    result = BitConverter.ToInt64(bytes, 0);
                    return(true);
                }
                return(false);

            case TypeCategory.UInt64:
                if (from.IsBytes)
                {
                    result = BitConverter.ToUInt64(bytes, 0);
                    return(true);
                }
                return(false);

            case TypeCategory.Single:
                if (from.IsBytes)
                {
                    result = BitConverter.ToSingle(bytes, 0);
                    return(true);
                }
                return(false);

            case TypeCategory.Double:
                if (from.IsBytes)
                {
                    result = BitConverter.ToDouble(bytes, 0);
                    return(true);
                }
                return(false);

            case TypeCategory.Interface:
            case TypeCategory.IntPtr:
            case TypeCategory.UIntPtr:
            case TypeCategory.Null:
            case TypeCategory.Others:
            default:
                return(false);
            }
        }
示例#11
0
        private static bool TryFromEnum(
            object value,
            XInfo from,
            XInfo to,
            out object result,
            string format)
        {
            result = GetDefaultValue(to.Category);

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

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

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

            case TypeCategory.String:
                result = IsToConverter.FromEnum.ToString(
                    from.Type,
                    value
                    );
                return(true);

            case TypeCategory.Stream:
                return(TryFromString(
                           value.ToString(),
                           XInfo.String.Value,
                           to,
                           out result,
                           format
                           ));

            case TypeCategory.Char:
                result = Convert.ToChar((int)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.IntPtr:
            case TypeCategory.UIntPtr:
            case TypeCategory.Interface:
            case TypeCategory.Class:
            case TypeCategory.Struct:
            case TypeCategory.Color:
            case TypeCategory.DateTime:
            case TypeCategory.Boolean:
            case TypeCategory.Null:
            case TypeCategory.Others:
            default:
                return(false);
            }
        }
示例#12
0
        private static bool TryFromStream(
            Stream 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.Stream:
                result = value;
                return(true);

            case TypeCategory.Enum:
            case TypeCategory.Color:
            case TypeCategory.Class:
            case TypeCategory.Struct:
            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.Int64:
            case TypeCategory.UInt64:
            case TypeCategory.Single:
            case TypeCategory.Double:
                using (var memoryStream = new MemoryStream()) {
                    value.CopyTo(memoryStream);
                    var bytes = memoryStream.ToArray();
                    return(TryFromArray(
                               bytes,
                               ToCache.Get(typeof(byte[])),
                               to,
                               out result,
                               format
                               ));
                }

            case TypeCategory.Interface:
            case TypeCategory.IntPtr:
            case TypeCategory.UIntPtr:
            case TypeCategory.Null:
            case TypeCategory.Others:
            default:
                return(false);
            }
        }
示例#13
0
        private static bool TryFromDateTime(
            DateTime value,
            XInfo from,
            XInfo to,
            out object result,
            string format)
        {
            result = GetDefaultValue(to.Category);

            switch (to.Category)
            {
            case TypeCategory.Array:
                if (to.IsBytes)
                {
                    result = value.ToBinary().To <byte[]>();
                    return(true);
                }
                return(TryToArray(
                           from,
                           to,
                           value,
                           out result,
                           format
                           ));

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

            case TypeCategory.String:
            case TypeCategory.Stream:
                var s = value.ToString(
                    string.IsNullOrEmpty(format)
                                                        ? "yyyy-MM-dd HH:mm:ss.fffffff"
                                                        : format
                    );
                return(TryFromString(
                           s,
                           XInfo.String.Value,
                           to,
                           out result,
                           format
                           ));

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

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

            case TypeCategory.Decimal:
            case TypeCategory.Byte:
            case TypeCategory.SByte:
            case TypeCategory.Int16:
            case TypeCategory.UInt16:
            case TypeCategory.Int32:
            case TypeCategory.UInt32:
            case TypeCategory.UInt64:
            case TypeCategory.Single:
            case TypeCategory.Double:
                return(false);

            case TypeCategory.Boolean:
            case TypeCategory.Enum:
            case TypeCategory.Color:
            case TypeCategory.Interface:
            case TypeCategory.Class:
            case TypeCategory.Struct:
            case TypeCategory.IntPtr:
            case TypeCategory.UIntPtr:
            case TypeCategory.Null:
            case TypeCategory.Others:
            default:
                return(false);
            }
        }