-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathProgram.cs
More file actions
365 lines (280 loc) · 14.7 KB
/
Program.cs
File metadata and controls
365 lines (280 loc) · 14.7 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Dynamic.Core.Parser;
using System.Linq.Expressions;
using System.Reflection;
using ExpressionTreeTestObjects;
using ExpressionTreeTestObjects.VB;
using ExpressionTreeToString;
using ZSpitz.Util;
using static System.Linq.Expressions.Expression;
namespace _tests {
class Program {
static void Main() {
//Expression<Func<Person, bool>> expr = p => p.DOB.DayOfWeek == DayOfWeek.Tuesday;
//Console.WriteLine(expr.ToString("C#"));
//Console.WriteLine(expr.ToString("Factory methods"));
//Expression<Func<string, bool>> equal = s => s == "test";
//LambdaExpression lambda = Expression.Lambda(equal.Body, Expression.Parameter(typeof(string), "s"));
//Console.WriteLine(equal.ToString("Factory methods"));
//Console.WriteLine(expr.ToString("Textual tree", Language.CSharp));
//Expression<Func<int, int, int>> expr = (i, j) => i * j;
//string s = expr.ToString("Factory methods", out Dictionary<string, (int start, int length)> pathSpans, "C#");
//const int firstColumnAlignment = -45;
//var s = expr.ToString("C#", out var pathSpans);
//Console.WriteLine(s);
//(int start, int length) = pathSpans["Body.Left.Operand"];
//Console.WriteLine(s.Substring(start, length));
//Console.WriteLine($"{"Path",firstColumnAlignment}Substring");
//Console.WriteLine(new string('-', 95));
//foreach (var kvp in pathSpans) {
// var path = kvp.Key;
// var (start, length) = kvp.Value;
// Console.WriteLine(
// $"{path,firstColumnAlignment}{new string(' ', start)}{s.Substring(start, length)}"
// );
//}
//expr = p => p.LastName.StartsWith("A");
//Console.WriteLine(expr.ToString("Factory methods", "Visual Basic"));
//var b = true;
//Expression<Func<bool>> expr = () => b;
//Console.WriteLine(expr.ToString("Object notation", "Visual Basic"));
//Expression<Func<bool>> expr = () => DateTime.Now.DayOfWeek == DayOfWeek.Monday;
//Console.WriteLine(expr.ToString("C#"));
//Expression<Func<int, int, string>> expr1 = (i, j) => (i + j + 5).ToString();
//Console.WriteLine(expr1.ToString("C#"));
//Console.WriteLine(expr1.ToString("Textual tree", "C#"));
//double d = 5.2;
//Expression<Func<string>> expr = () => ((int)d).ToString();
//Console.WriteLine(expr.ToString("C#"));
//var b = true;
//Expression<Func<int>> expr = () => b ? 1 : 0;
//Console.WriteLine(expr.ToString("ToString"));
//Console.WriteLine(expr.ToString("DebugView"));
//Console.WriteLine(expr.ToString("ToString"));
//Console.WriteLine(expr.ToString("Factory methods", "C#"));
//var dow = DayOfWeek.Sunday;
//Expression<Func<bool>> expr = () => DateTime.Today.DayOfWeek == dow;
//Console.WriteLine(expr.ToString("Textual tree", "C#"));
//Console.WriteLine(expr.ToString("C#"));
//Expression<Func<IEnumerable<char>>> expr = () => (IEnumerable<char>)"abcd";
//Console.WriteLine(expr.ToString("C#"));
//Loader.Load();
////Expression x = (Expression)Objects.Get().Where(x => x.o is Expression expr && expr.CanReduce).Select(x => x.o).FirstOrDefault();
////Console.WriteLine(x.ToString("Textual tree", "C#"));
//Objects.Get()
// .Select(x => (o: x.o as Expression, x.name))
// .Where(x => x.o is Expression expr && expr.CanReduce)
// .Select(x => {
// (Expression expr, string name) = (x.o!, x.name);
// TextualTreeWriterVisitor.ReducePredicate = _ => false;
// var unreduced = expr.ToString("Textual tree", "C#");
// TextualTreeWriterVisitor.ReducePredicate = null;
// var @default = expr.ToString("Textual tree", "C#");
// TextualTreeWriterVisitor.ReducePredicate = _ => true;
// var allReduced = expr.ToString("Textual tree", "C#");
// return (name, unreduced, @default, allReduced);
// }).ForEach(x => {
// var (name, unreduced, @default, allReduced) = x;
// Console.WriteLine($"======== {name} - {(@default != unreduced ? "default " : "")}{(allReduced != @default ? "allReduced" : "")}");
// Console.WriteLine(unreduced);
// if (@default != unreduced) { Console.WriteLine(@default); }
// if (allReduced != @default) { Console.WriteLine(allReduced); }
// });
//bool? b = true;
//Expression<Func<object>> expr = () => !b;
//Console.WriteLine(expr.ToString("C#"));
//Expression<Func<Foo, Foo?>> expr = f =>
// f != null && f.A != null && f.A.B != null && f.A.B.C != null ?
// f.A.B.C.D :
// null;
//Console.WriteLine(expr.ToString("Dynamic LINQ"));
////Expression<Func<Person, string>> expr = p => p.TestExtension();
//availableRenderersSamples();
//var s = "123";
//int i = 5;
//Expression<Func<bool>> expr = () => int.TryParse(s, out i);
//var s1 = expr.ToString("Factory methods", "C#");
//var Bar = Parameter(
// typeof(string),
// "Bar"
//);
//var Baz = Parameter(
// typeof(string),
// "Baz"
//);
//var s = "123";
//var i = Variable(typeof(int), "i");
//var expr2 = Lambda(
// Call(
// typeof(int).GetMethod("TryParse", new[] { typeof(string), typeof(int).MakeByRefType() }),
// Constant(s),
// i
// )
//);
//int i = 5;
//Expression<Func<string>> expr = () => i.ToString();
//Console.WriteLine(expr.ToString("Factory methods", "C#"));
//int i = 5;
//Expression<Func<long>> expr = () => (long)i;
//Console.WriteLine(expr.ToString("Textual tree", "C#"));
//Console.WriteLine(expr.ToString("C#"));
//Expression expr = Equal(
// ConvertChecked(
// Constant(DayOfWeek.Tuesday),
// typeof(int)
// ),
// ConvertChecked(
// Constant(DayOfWeek.Monday),
// typeof(int)
// )
//);
//Expression<Func<Person, bool>> expr = p => p.DOB!.DayOfWeek == DayOfWeek.Thursday || p.DOB!.DayOfWeek == DayOfWeek.Wednesday;
//IQueryable<Person> qry = new List<Person>().AsQueryable();
//qry = qry.Where(p => p.LastName.StartsWith("A") || p.LastName.EndsWith("Z"));
////Console.WriteLine(qry.Expression.ToString("Factory methods", "C#"));
//Console.WriteLine(qry.Expression.ToString("Object notation", "C#"));
// Console.WriteLine(expr.ToString("Textual tree", "C#"));
// Console.WriteLine(expr.ToString("C#"));
// Console.WriteLine(expr.ToString("Dynamic LINQ"));
//Expression<Func<Person, bool>> expr = p => p.LastName.StartsWith("A");
//Console.WriteLine(expr.ToString("Dynamic LINQ"));
//Console.WriteLine(expr.ToString("C#"));
//Console.WriteLine(expr.ToString("Textual tree", "C#"));
//Console.WriteLine(expr.ToString("Factory methods", "C#"));
//var qry = new List<User>().AsQueryable().Where(x => x.UserName.Where(c => c.ToString() == "a").Any()).OrderBy(x => x.UserName);
// #List.AsQueryable().Where("UserName.Where(ToString() = \"a\")")
//Console.WriteLine(qry.Expression.ToString("C#"));
//Console.WriteLine(qry.Expression.ToString("Visual Basic"));
//Console.WriteLine(qry.Expression.ToString("Dynamic LINQ"));
//Expression<Func<string>> expr1 = () => "";
//Console.WriteLine(debugView.GetValue(expr1));
//Console.WriteLine(expr1.ToString("DebugView"));
//Expression<Func<Expression<Func<string>>>> expr2 = () => () => "";
//Console.WriteLine(debugView.GetValue(expr2));
//Console.WriteLine(expr2.ToString("DebugView"));
//Expression<Func<Expression<Func<Expression<Func<string>>>>>> expr3 = () => () => () => "";
//Console.WriteLine(debugView.GetValue(expr3));
//Console.WriteLine(expr3.ToString("DebugView"));
//Expression<Func<bool>> expr = () => "abcd"[0] > 'c';
//Console.WriteLine(expr.ToString("C#", out var pathSpans));
//Console.WriteLine(expr.ToString("Visual Basic"));
//Console.WriteLine(expr.ToString("Dynamic LINQ"));
//Expression expr1 = Lambda(
// Equal(
// Property(
// Constant("abcd"),
// typeof(string).GetProperty("Chars"),
// Constant(0)
// ),
// Constant('c')
// )
// );
//Console.WriteLine(expr1.ToString("C#"));
//Console.WriteLine(expr1.ToString("Visual Basic"));
//Console.WriteLine(expr1.ToString("Dynamic LINQ"));
//Console.WriteLine(expr1.ToString("Factory methods", "C#"));
//Expression<Func<Person, bool>> expr = p => p.LastName![0] == 'c' || p.LastName[0] == 'd';
//Console.WriteLine(expr.ToString("Dynamic LINQ"));
//IFormatProvider provider = CultureInfo.CurrentCulture;
//IFormatProvider provider1 = CultureInfo.GetCultureInfo("he-IL");
////var selector = "ToString().ToString(@0).ToString(@0)[0] in ('c','d')";
//var selector = "np(ToString().ToString(@0).ToString(@1))";
//var prm = Parameter(typeof(Person));
//var parser = new ExpressionParser(new[] { prm }, selector, new object[] { provider, provider1 }, ParsingConfig.Default);
//var expr1 = parser.Parse(null);
//Console.WriteLine(expr1.ToString("Dynamic LINQ"));
//Expression<Func<Employee, string?>> firstNameExpression = e => e.FirstName;
//Console.WriteLine(firstNameExpression.ToString("Textual tree", "C#"));
//var expr =
// Enumerable.Empty<Person>()
// .AsQueryable()
// .Where(x => x.Age <= 20)
// .OrderBy(x => x != null && x.LastName != null ? x.LastName : "")
// .ThenBy(x => x != null && x.FirstName != null ? x.FirstName : "")
// .Expression;
//Console.WriteLine(expr.ToString("Dynamic LINQ", "C#"));
//var prm = Parameter(typeof(string));
//var expr = Lambda(
// Equal(prm, prm),
// prm
//);
//Console.WriteLine(expr.ToString("Factory methods", "C#"));
Expression<Func<Person, bool>> filter = p => p.LastName == "A" || p.FirstName == "B" || p.DOB == DateTime.MinValue || p.LastName == "C" || p.FirstName == "D";
Console.WriteLine(filter.ToString("Dynamic LINQ", "C#"));
//IQueryable<Person> query = new List<Person>()
// .AsQueryable()
// .Where(p => p.LastName == "A" || p.FirstName == "B" || p.DOB == DateTime.MinValue || p.LastName == "C" || p.FirstName == "D");
//Console.WriteLine(query.Expression.ToString("Dynamic LINQ", "C#"));
//Console.WriteLine(filter.Body.ToString("Dynamic LINQ", "C#"));
}
//class TestContainer { }
//class SomeType {
// public object Array(double[] arr) => throw new NotImplementedException();
// public object Max(TestContainer tc)
//}
static readonly PropertyInfo debugView = typeof(Expression).GetProperty("DebugView", BindingFlags.NonPublic | BindingFlags.Instance)!;
static void availableRenderersSamples() {
static void line() => Console.WriteLine(new string('=', 50));
Expression<Func<Person, bool>> expr = p => p.DOB!.Value.DayOfWeek == DayOfWeek.Tuesday;
Console.WriteLine(expr.ToString("C#"));
line();
Console.WriteLine(expr.ToString("Visual Basic"));
line();
Console.WriteLine(expr.ToString("Factory methods", "C#"));
line();
Expression<Func<Person, bool>> expr1 = p => p.DOB!.Value.DayOfWeek == DayOfWeek.Tuesday || p.DOB!.Value.DayOfWeek == DayOfWeek.Thursday;
Console.WriteLine(expr1.ToString("Dynamic LINQ"));
line();
Expression<Func<Person, bool>> expr2 = p => true;
Console.WriteLine(expr2.ToString("Textual tree", "C#"));
line();
Console.WriteLine(expr2.ToString("Object notation", "C#"));
line();
Console.WriteLine(expr2.ToString("ToString"));
line();
Console.WriteLine(expr2.ToString("DebugView"));
}
}
public class Person {
public string LastName { get; set; } = "";
public string FirstName { get; set; } = "";
public string Name => LastName + ", " + FirstName;
public DateTime? DOB { get; set; }
public Post[] Posts { get; set; } = new Post[] { };
public char GetChar() => LastName[0];
public int? Age {
get {
if (DOB is null) { return null; }
var dob = DOB.Value;
var today = DateTime.Today;
var age = today.Year - dob.Year;
if (dob.Date > today.AddYears(-age)) { age--; }
return age;
}
}
}
public class Employee {
public string? FirstName { get; set; }
public string? LastName { get; set; }
}
public class Post {
public string Text { get; set; } = "";
public string[]? Tags { get; set; }
}
public static class Extension {
public static string TestExtension(this Person p) => "";
}
class Foo {
public Foo? A { get; set; }
public Foo? B { get; set; }
public Foo? C { get; set; }
public Foo? D { get; set; }
}
class User {
public string UserName { get; set; } = "";
}
}