|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using LinqToDB.Extensions; |
| 4 | + |
| 5 | +namespace LinqToDB.Common |
| 6 | +{ |
| 7 | + internal static class ConvertUtils |
| 8 | + { |
| 9 | + private static readonly IDictionary<Type, ISet<Type>> _alwaysConvert = new Dictionary<Type, ISet<Type>>() |
| 10 | + { |
| 11 | + { typeof(byte) , new HashSet<Type>() { typeof(short), typeof(ushort), typeof(int) , typeof(uint), typeof(long), typeof(ulong) } }, |
| 12 | + { typeof(sbyte) , new HashSet<Type>() { typeof(short), typeof(ushort), typeof(int) , typeof(uint), typeof(long), typeof(ulong) } }, |
| 13 | + { typeof(short) , new HashSet<Type>() { typeof(int) , typeof(uint) , typeof(long), typeof(ulong) } }, |
| 14 | + { typeof(ushort), new HashSet<Type>() { typeof(int) , typeof(uint) , typeof(long), typeof(ulong) } }, |
| 15 | + { typeof(int) , new HashSet<Type>() { typeof(long) , typeof(ulong) } }, |
| 16 | + { typeof(uint) , new HashSet<Type>() { typeof(long) , typeof(ulong) } }, |
| 17 | + { typeof(long) , new HashSet<Type>() { } }, |
| 18 | + { typeof(ulong) , new HashSet<Type>() { } }, |
| 19 | + }; |
| 20 | + |
| 21 | + private static readonly IDictionary<Type, IDictionary<Type, Tuple<IComparable, IComparable>>> _rangedConvert = new Dictionary<Type, IDictionary<Type, Tuple<IComparable, IComparable>>>() |
| 22 | + { |
| 23 | + { typeof(byte) , new Dictionary<Type, Tuple<IComparable, IComparable>>() |
| 24 | + { |
| 25 | + { typeof(sbyte), Tuple.Create((IComparable)(byte)0, (IComparable)(byte)sbyte.MaxValue) } |
| 26 | + } |
| 27 | + }, |
| 28 | + { typeof(sbyte) , new Dictionary<Type, Tuple<IComparable, IComparable>>() |
| 29 | + { |
| 30 | + { typeof(byte), Tuple.Create((IComparable)(sbyte)0, (IComparable)sbyte.MaxValue) } |
| 31 | + } |
| 32 | + }, |
| 33 | + { typeof(short) , new Dictionary<Type, Tuple<IComparable, IComparable>>() |
| 34 | + { |
| 35 | + { typeof(sbyte) , Tuple.Create((IComparable)(short)sbyte.MinValue, (IComparable)(short)sbyte.MaxValue) }, |
| 36 | + { typeof(byte ) , Tuple.Create((IComparable)(short)byte .MinValue, (IComparable)(short)byte .MaxValue) }, |
| 37 | + { typeof(ushort), Tuple.Create((IComparable)(short)0 , (IComparable) short.MaxValue) }, |
| 38 | + } |
| 39 | + }, |
| 40 | + { typeof(ushort) , new Dictionary<Type, Tuple<IComparable, IComparable>>() |
| 41 | + { |
| 42 | + { typeof(sbyte) , Tuple.Create((IComparable)(ushort)0, (IComparable)(ushort)sbyte.MaxValue) }, |
| 43 | + { typeof(byte ) , Tuple.Create((IComparable)(ushort)0, (IComparable)(ushort)byte .MaxValue) }, |
| 44 | + { typeof(short) , Tuple.Create((IComparable)(ushort)0, (IComparable)(ushort)short.MaxValue) }, |
| 45 | + } |
| 46 | + }, |
| 47 | + { typeof(int) , new Dictionary<Type, Tuple<IComparable, IComparable>>() |
| 48 | + { |
| 49 | + { typeof(sbyte) , Tuple.Create((IComparable)(int)sbyte .MinValue, (IComparable)(int)sbyte .MaxValue) }, |
| 50 | + { typeof(byte ) , Tuple.Create((IComparable)(int)byte .MinValue, (IComparable)(int)byte .MaxValue) }, |
| 51 | + { typeof(short) , Tuple.Create((IComparable)(int)short .MinValue, (IComparable)(int)short .MaxValue) }, |
| 52 | + { typeof(ushort), Tuple.Create((IComparable)(int)ushort.MinValue, (IComparable)(int)ushort .MaxValue) }, |
| 53 | + { typeof(uint) , Tuple.Create((IComparable)(int)0 , (IComparable) int .MaxValue) }, |
| 54 | + } |
| 55 | + }, |
| 56 | + { typeof(uint) , new Dictionary<Type, Tuple<IComparable, IComparable>>() |
| 57 | + { |
| 58 | + { typeof(sbyte ), Tuple.Create((IComparable)(uint)0, (IComparable)(uint)sbyte .MaxValue) }, |
| 59 | + { typeof(byte ), Tuple.Create((IComparable)(uint)0, (IComparable)(uint)byte .MaxValue) }, |
| 60 | + { typeof(short ), Tuple.Create((IComparable)(uint)0, (IComparable)(uint)short .MaxValue) }, |
| 61 | + { typeof(ushort), Tuple.Create((IComparable)(uint)0, (IComparable)(uint)ushort.MaxValue) }, |
| 62 | + { typeof(int ), Tuple.Create((IComparable)(uint)0, (IComparable)(uint)int .MaxValue) }, |
| 63 | + } |
| 64 | + }, |
| 65 | + { typeof(long) , new Dictionary<Type, Tuple<IComparable, IComparable>>() |
| 66 | + { |
| 67 | + { typeof(sbyte ), Tuple.Create((IComparable)(long)sbyte .MinValue, (IComparable)(long)sbyte .MaxValue) }, |
| 68 | + { typeof(byte ), Tuple.Create((IComparable)(long)byte .MinValue, (IComparable)(long)byte .MaxValue) }, |
| 69 | + { typeof(short ), Tuple.Create((IComparable)(long)short .MinValue, (IComparable)(long)short .MaxValue) }, |
| 70 | + { typeof(ushort), Tuple.Create((IComparable)(long)ushort.MinValue, (IComparable)(long)ushort.MaxValue) }, |
| 71 | + { typeof(int ), Tuple.Create((IComparable)(long)int .MinValue, (IComparable)(long)int .MaxValue) }, |
| 72 | + { typeof(uint ), Tuple.Create((IComparable)(long)uint .MinValue, (IComparable)(long)uint .MaxValue) }, |
| 73 | + { typeof(ulong ), Tuple.Create((IComparable)(long)0 , (IComparable) long .MaxValue) }, |
| 74 | + } |
| 75 | + }, |
| 76 | + { typeof(ulong) , new Dictionary<Type, Tuple<IComparable, IComparable>>() |
| 77 | + { |
| 78 | + { typeof(sbyte ), Tuple.Create((IComparable)(ulong)0, (IComparable)(ulong)sbyte .MaxValue) }, |
| 79 | + { typeof(byte ), Tuple.Create((IComparable)(ulong)0, (IComparable)(ulong)byte .MaxValue) }, |
| 80 | + { typeof(short ), Tuple.Create((IComparable)(ulong)0, (IComparable)(ulong)short .MaxValue) }, |
| 81 | + { typeof(ushort), Tuple.Create((IComparable)(ulong)0, (IComparable)(ulong)ushort.MaxValue) }, |
| 82 | + { typeof(int ), Tuple.Create((IComparable)(ulong)0, (IComparable)(ulong)int .MaxValue) }, |
| 83 | + { typeof(uint ), Tuple.Create((IComparable)(ulong)0, (IComparable)(ulong)uint .MaxValue) }, |
| 84 | + { typeof(long ), Tuple.Create((IComparable)(ulong)0, (IComparable)(ulong)long .MaxValue) }, |
| 85 | + } |
| 86 | + }, |
| 87 | + }; |
| 88 | + |
| 89 | + public static bool TryConvert(object value, Type toType, out object convertedValue) |
| 90 | + { |
| 91 | + convertedValue = null; |
| 92 | + |
| 93 | + if (value == null) |
| 94 | + return toType.IsClassEx() || toType.IsNullable(); |
| 95 | + |
| 96 | + var from = value.GetType().ToNullableUnderlying(); |
| 97 | + var to = toType .ToNullableUnderlying(); |
| 98 | + |
| 99 | + if (from == to) |
| 100 | + { |
| 101 | + convertedValue = value; |
| 102 | + return true; |
| 103 | + } |
| 104 | + |
| 105 | + if (_alwaysConvert.TryGetValue(from, out var types) && types.Contains(to)) |
| 106 | + { |
| 107 | + convertedValue = Convert.ChangeType(value, to); |
| 108 | + return true; |
| 109 | + } |
| 110 | + |
| 111 | + if (!_rangedConvert.TryGetValue(from, out var rangedTypes) |
| 112 | + || !rangedTypes.TryGetValue(to, out var ranges)) |
| 113 | + return false; |
| 114 | + |
| 115 | + if ( ranges.Item1.CompareTo(value) <= 0 |
| 116 | + && ranges.Item2.CompareTo(value) >= 0) |
| 117 | + { |
| 118 | + convertedValue = Convert.ChangeType(value, to); |
| 119 | + return true; |
| 120 | + } |
| 121 | + |
| 122 | + return false; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments