Repository navigation
Expand file tree
/
Copy pathInspectUtils.cs
More file actions
81 lines (66 loc) · 2.73 KB
/
Copy pathInspectUtils.cs
File metadata and controls
81 lines (66 loc) · 2.73 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
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace ServiceStack.Html;
public static class InspectUtils
{
public static Expression? FindMember(Expression e) => e switch {
UnaryExpression ue => FindMember(ue.Operand),
MemberExpression me => me,
_ => null
};
public static PropertyInfo? PropertyFromExpression<TModel>(Expression<Func<TModel, object?>> expr)
{
var memberExpr = FindMember(expr.Body) as MemberExpression;
return memberExpr?.Member is PropertyInfo pi ? pi : null;
}
// Evaluates an expression (not a LambdaExpression), e.g. 2 + 2.
public static object Evaluate(Expression arg)
{
if (arg == null)
throw new ArgumentNullException(nameof(arg));
var func = Wrap(arg);
return func(null);
}
private static readonly ParameterExpression _unusedParameterExpr = Expression.Parameter(typeof(object), "_unused");
private static Func<object?, object?> Wrap(Expression arg)
{
var lambdaExpr = Expression.Lambda<Func<object?, object?>>(Expression.Convert(arg, typeof(object)), _unusedParameterExpr);
return lambdaExpr.Compile();
}
public static string[] GetFieldNames<T>(this Expression<Func<T, object>> expr)
{
if (expr.Body is MemberExpression member)
{
if (member.Member?.DeclaringType?.IsAssignableFrom(typeof(T)) == true)
return new[] { member.Member.Name };
var array = Evaluate(member);
if (array is IEnumerable<string> strEnum)
return strEnum.ToArray();
}
if (expr.Body is NewExpression newExpr)
return newExpr.Arguments.OfType<MemberExpression>().Select(x => x.Member.Name).ToArray();
if (expr.Body is MemberInitExpression init)
return init.Bindings.Select(x => x.Member.Name).ToArray();
if (expr.Body is NewArrayExpression newArray)
{
var constantExprs = newArray.Expressions.OfType<ConstantExpression>().ToList();
if (newArray.Expressions.Count == constantExprs.Count)
return constantExprs.Select(x => x.Value.ToString()).ToArray();
var array = Evaluate(newArray);
if (array is string[] strArray)
return strArray;
return array.ConvertTo<string[]>();
}
if (expr.Body is UnaryExpression unary)
{
var unaryMember = unary.Operand as MemberExpression;
if (unaryMember != null)
return new[] { unaryMember.Member.Name };
}
throw new ArgumentException("Invalid Fields List Expression: " + expr);
}
}