|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Globalization; |
| 4 | +using System.Linq; |
| 5 | +using System.Windows.Data; |
| 6 | +using System.Windows.Markup; |
| 7 | + |
| 8 | +namespace RegexDialog.Converters |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// MultiBinding Converter that use a string mathematical or pseudo C# expression to make the conversion. |
| 12 | + /// Use <c>bindings</c> as an array of object to inject bindings values in the expression (example <c>Abs(bindings[0]) + bindings[1]</c>) |
| 13 | + /// </summary> |
| 14 | + [ContentProperty("Expression")] |
| 15 | + public class ExpressionEvalMultiBindingConverter : BaseConverter, IMultiValueConverter |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// To specify a list of namespaces separated by the ; character to add as usings for the evaluator |
| 19 | + /// </summary> |
| 20 | + public string NamespacesToAdd { get; set; } = string.Empty; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// The expression to evaluate to make the conversion. Use <c>bindings</c> as an array of object to inject bindings values in the expression. By default just <c>bindings</c> |
| 24 | + /// </summary> |
| 25 | + public string Expression { get; set; } = "bindings"; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// The expression to evaluate to make the back conversion. Use <c>binding</c> to inject the binding value in the expression. By default just <c>binding</c> |
| 29 | + /// Must return an array of object |
| 30 | + /// </summary> |
| 31 | + public string ExpressionForConvertBack { get; set; } = "binding"; |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// If <c>>= 0</c> evaluate the binding at corresponding index as an expression, if just inject the binding in the Expression, By default : <c>-1</c> |
| 35 | + /// </summary> |
| 36 | + public int EvaluateBindingAtIndexAsAnExpression { get; set; } = -1; |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// If <c>true</c> evaluate a string binding as an expression, if <c>false</c> just inject the binding in the ExpressionForConvertBack, By default : <c>false</c> |
| 40 | + /// </summary> |
| 41 | + public bool EvaluateBindingAsAnExpressionForConvertBack { get; set; } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// If <c>true</c> Evaluate function is callables in an expression. If <c>false</c> Evaluate is not callable. |
| 45 | + /// By default : false for security |
| 46 | + /// </summary> |
| 47 | + public bool OptionEvaluateFunctionActive { get; set; } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// if true all evaluation are case sensitives, if false evaluations are case insensitive. |
| 51 | + /// By default = true |
| 52 | + /// </summary> |
| 53 | + public bool OptionCaseSensitiveEvaluationActive { get; set; } = true; |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// If <c>true</c> throw up all evaluate exceptions, if <c>false</c> just return the exception message as a string, By default <c>false</c> |
| 57 | + /// </summary> |
| 58 | + public bool ThrowExceptions { get; set; } |
| 59 | + |
| 60 | + public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) |
| 61 | + { |
| 62 | + Dictionary<string, object> variables = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); |
| 63 | + |
| 64 | + try |
| 65 | + { |
| 66 | + ExpressionEvaluator evaluator = new ExpressionEvaluator() |
| 67 | + { |
| 68 | + OptionCaseSensitiveEvaluationActive = OptionCaseSensitiveEvaluationActive |
| 69 | + }; |
| 70 | + |
| 71 | + evaluator.Namespaces.NamespacesListForWPFConverters(); |
| 72 | + |
| 73 | + NamespacesToAdd.Split(';').ToList().ForEach(namespaceName => |
| 74 | + { |
| 75 | + if (!string.IsNullOrWhiteSpace(namespaceName)) |
| 76 | + { |
| 77 | + evaluator.Namespaces.Add(namespaceName); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + evaluator.OptionEvaluateFunctionActive = OptionEvaluateFunctionActive; |
| 82 | + |
| 83 | + variables["bindings"] = values; |
| 84 | + |
| 85 | + evaluator.Variables = variables; |
| 86 | + |
| 87 | + if (EvaluateBindingAtIndexAsAnExpression >= 0) |
| 88 | + { |
| 89 | + return evaluator.Evaluate(values[EvaluateBindingAtIndexAsAnExpression].ToString()); |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + return evaluator.Evaluate(Expression); |
| 94 | + } |
| 95 | + } |
| 96 | + catch (Exception ex) |
| 97 | + { |
| 98 | + if (ThrowExceptions) |
| 99 | + { |
| 100 | + throw; |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + return ex.Message; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) |
| 110 | + { |
| 111 | + Dictionary<string, object> variables = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); |
| 112 | + |
| 113 | + try |
| 114 | + { |
| 115 | + ExpressionEvaluator evaluator = new ExpressionEvaluator() |
| 116 | + { |
| 117 | + OptionCaseSensitiveEvaluationActive = OptionCaseSensitiveEvaluationActive |
| 118 | + }; |
| 119 | + |
| 120 | + evaluator.Namespaces.NamespacesListForWPFConverters(); |
| 121 | + |
| 122 | + evaluator.OptionEvaluateFunctionActive = OptionEvaluateFunctionActive; |
| 123 | + |
| 124 | + if (EvaluateBindingAsAnExpressionForConvertBack) |
| 125 | + { |
| 126 | + variables["binding"] = evaluator.Evaluate(value.ToString()); |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + variables["binding"] = value; |
| 131 | + } |
| 132 | + |
| 133 | + evaluator.Variables = variables; |
| 134 | + |
| 135 | + return (object[])evaluator.Evaluate(ExpressionForConvertBack); |
| 136 | + } |
| 137 | + catch (Exception ex) |
| 138 | + { |
| 139 | + if (ThrowExceptions) |
| 140 | + { |
| 141 | + throw; |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + return new object[] { ex.Message }; |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments