|
| 1 | +namespace XrmTools.Shell.Conveters; |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Globalization; |
| 5 | +using System.Windows.Data; |
| 6 | +using XrmTools.Shell.Styles; |
| 7 | + |
| 8 | +internal class NumericComparisonConverter : IValueConverter, IMultiValueConverter |
| 9 | +{ |
| 10 | + public Operation Operation { get; set; } |
| 11 | + |
| 12 | + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
| 13 | + { |
| 14 | + bool flag; |
| 15 | + switch (value) |
| 16 | + { |
| 17 | + case byte num1: |
| 18 | + flag = Compare(num1, System.Convert.ToByte(parameter)); |
| 19 | + break; |
| 20 | + case double num2: |
| 21 | + flag = Compare(num2, System.Convert.ToDouble(parameter)); |
| 22 | + break; |
| 23 | + case float num3: |
| 24 | + flag = Compare(num3, System.Convert.ToSingle(parameter)); |
| 25 | + break; |
| 26 | + case int num4: |
| 27 | + flag = Compare(num4, System.Convert.ToInt32(parameter)); |
| 28 | + break; |
| 29 | + case long num5: |
| 30 | + flag = Compare(num5, System.Convert.ToInt64(parameter)); |
| 31 | + break; |
| 32 | + case sbyte num6: |
| 33 | + flag = Compare(num6, System.Convert.ToSByte(parameter)); |
| 34 | + break; |
| 35 | + case short num7: |
| 36 | + flag = Compare(num7, System.Convert.ToInt16(parameter)); |
| 37 | + break; |
| 38 | + case uint num8: |
| 39 | + flag = Compare(num8, System.Convert.ToUInt32(parameter)); |
| 40 | + break; |
| 41 | + case ulong num9: |
| 42 | + flag = Compare(num9, System.Convert.ToUInt64(parameter)); |
| 43 | + break; |
| 44 | + case ushort num10: |
| 45 | + flag = Compare(num10, System.Convert.ToUInt16(parameter)); |
| 46 | + break; |
| 47 | + default: |
| 48 | + throw new NotSupportedException(value.GetType().ToString()); |
| 49 | + } |
| 50 | + return flag; |
| 51 | + } |
| 52 | + |
| 53 | + public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) |
| 54 | + { |
| 55 | + if (values.Length != 2) |
| 56 | + throw new ArgumentException(nameof(values)); |
| 57 | + return Convert(values[0], targetType, values[1], culture); |
| 58 | + } |
| 59 | + |
| 60 | + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
| 61 | + { |
| 62 | + throw new NotImplementedException(); |
| 63 | + } |
| 64 | + |
| 65 | + public object[] ConvertBack( |
| 66 | + object value, |
| 67 | + Type[] targetTypes, |
| 68 | + object parameter, |
| 69 | + CultureInfo culture) |
| 70 | + { |
| 71 | + throw new NotImplementedException(); |
| 72 | + } |
| 73 | + |
| 74 | + private bool Compare<T>(T value, T compareValue) where T : IComparable<T> |
| 75 | + { |
| 76 | + switch (Operation) |
| 77 | + { |
| 78 | + case Operation.IsEqual: |
| 79 | + return value.CompareTo(compareValue) == 0; |
| 80 | + case Operation.IsNotEqual: |
| 81 | + return value.CompareTo(compareValue) != 0; |
| 82 | + case Operation.IsGreaterThan: |
| 83 | + return value.CompareTo(compareValue) > 0; |
| 84 | + case Operation.IsGreaterThanOrEqual: |
| 85 | + return value.CompareTo(compareValue) >= 0; |
| 86 | + case Operation.IsLessThan: |
| 87 | + return value.CompareTo(compareValue) < 0; |
| 88 | + case Operation.IsLessThanOrEqual: |
| 89 | + return value.CompareTo(compareValue) <= 0; |
| 90 | + default: |
| 91 | + throw new NotSupportedException(Operation.ToString()); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments