-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicExpressions.cs
More file actions
163 lines (137 loc) · 6.05 KB
/
Copy pathDynamicExpressions.cs
File metadata and controls
163 lines (137 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.ComponentModel;
namespace Dynamic_If
{
internal delegate object PropertySelector<T>(T data, string str);
internal class DynamicExpressions<T>
{
private PropertySelector<T> _propSelector;
public DynamicExpressions(PropertySelector<T> ps) { _propSelector = ps; }
public BindingList<T> Evaluate(BindingList<T> data, string filterExpression)
{
List<T> temp = data.ToList();
return new BindingList<T>(temp.Where(temp => EvaluateExpression(temp, filterExpression)).ToList());
}
private bool EvaluateExpression(T data, string expression)
{
expression = expression.Replace("\"", "");
while (expression.Contains('('))
{
int openIndex = expression.LastIndexOf('(');
int closeIndex = expression.IndexOf(')', openIndex);
if (closeIndex == -1)
throw new ArgumentException("Mismatched parentheses");
string subExpression = expression.Substring(openIndex + 1, closeIndex - openIndex - 1);
bool result = ExprsByOr(data, subExpression);
expression = expression.Substring(0, openIndex) + result.ToString() + expression.Substring(closeIndex + 1);
}
return ExprsByOr(data, expression);
}
private bool ExprsByOr(T data, string expression)
{
var exprs = expression.Split(new[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var expr in exprs)
{
var trimmed = expr.Trim();
if (ExprsByAnd(data, trimmed))
return true;
}
return false;
}
private bool ExprsByAnd(T data, string expression)
{
var exprs = expression.Split(new[] { "&&" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var expr in exprs)
{
var trimmed = expr.Trim();
string pattern = @"([\w]+)\s*(==|!=|>|>=|<|<=)\s*([-\w,]+)";
var match = Regex.Match(trimmed, pattern, RegexOptions.IgnoreCase);
bool res = false;
if (!match.Success)
{
pattern = @"(\w+)";
match = Regex.Match(trimmed, pattern, RegexOptions.IgnoreCase);
if (!match.Success)
throw new ArgumentException("Invalid expression: " + trimmed);
if (bool.TryParse(match.Groups[0].Value, out res))
{
if (match.Groups.Count == 2 && res == false)
return res;
else
continue;
}
}
var property = match.Groups[1].Value;
var op = match.Groups[2].Value;
var value = match.Groups[3].Value;
object propValue = _propSelector(data, property);
//switch (property)
//{
// case "a": propValue = data.a; break;
// case "b": propValue = data.b; break;
// case "c": propValue = data.c; break;
// case "d": propValue = data.d; break;
// case "e": propValue = data.e; break;
// case "true": propValue = true; break;
// case "false": propValue = false; break;
// default: throw new ArgumentException("Invalid property: " + property);
//}
if (!EvaluateComparison(propValue, op, value))
return false;
}
return true;
}
private bool EvaluateComparison(object propValue, string op, string value)
{
switch (propValue)
{
case int intVal:
int intComp = int.Parse(value);
return EvaluateValue<int>(intVal, op, intComp);
case byte byteVal:
byte byteComp = byte.Parse(value);
return EvaluateValue<byte>(byteVal, op, byteComp);
case float floatVal:
float floatComp = float.Parse(value);
return EvaluateValue<float>(floatVal, op, floatComp);
case UInt16 uintVal:
UInt16 uintComp = UInt16.Parse(value);
return EvaluateValue<UInt16>(uintVal, op, uintComp);
case string strVal:
return EvaluateValue<string>(strVal, op, value);
case bool boolVal:
bool boolComp = bool.Parse(value);
return EvaluateValue<bool>(boolVal, op, boolComp);
default:
throw new ArgumentException("Unsupported property type");
}
}
private bool EvaluateValue<T1>(T1 propValue, string op, T1 value) where T1 : IComparable<T1>
{
if (typeof(T1) == typeof(string) || typeof(T1) == typeof(bool))
{
switch (op)
{
case "==": return propValue.CompareTo(value) == 0;
case "!=": return propValue.CompareTo(value) != 0;
default: throw new ArgumentException("Invalid operator: " + op);
}
}
switch (op)
{
case "==": return propValue.CompareTo(value) == 0;
case "!=": return propValue.CompareTo(value) != 0;
case ">": return propValue.CompareTo(value) > 0;
case ">=": return propValue.CompareTo(value) >= 0;
case "<": return propValue.CompareTo(value) < 0;
case "<=": return propValue.CompareTo(value) <= 0;
default: throw new ArgumentException("Invalid operator: " + op);
}
}
}
}