This repository was archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathExpressionHelper.cs
139 lines (121 loc) · 5.47 KB
/
ExpressionHelper.cs
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
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.AspNet.Mvc.Core;
namespace Microsoft.AspNet.Mvc.Rendering.Expressions
{
public static class ExpressionHelper
{
public static string GetExpressionText(string expression)
{
// If it's exactly "model", then give them an empty string, to replicate the lambda behavior.
return
string.Equals(expression, "model", StringComparison.OrdinalIgnoreCase)
? string.Empty
: expression;
}
public static string GetExpressionText([NotNull] LambdaExpression expression)
{
// Split apart the expression string for property/field accessors to create its name
var nameParts = new Stack<string>();
var part = expression.Body;
while (part != null)
{
if (part.NodeType == ExpressionType.Call)
{
var methodExpression = (MethodCallExpression)part;
if (!IsSingleArgumentIndexer(methodExpression))
{
break;
}
nameParts.Push(
GetIndexerInvocation(
methodExpression.Arguments.Single(),
expression.Parameters.ToArray()));
part = methodExpression.Object;
}
else if (part.NodeType == ExpressionType.ArrayIndex)
{
var binaryExpression = (BinaryExpression)part;
nameParts.Push(
GetIndexerInvocation(
binaryExpression.Right,
expression.Parameters.ToArray()));
part = binaryExpression.Left;
}
else if (part.NodeType == ExpressionType.MemberAccess)
{
var memberExpressionPart = (MemberExpression)part;
nameParts.Push("." + memberExpressionPart.Member.Name);
part = memberExpressionPart.Expression;
}
else if (part.NodeType == ExpressionType.Parameter)
{
// When the expression is parameter based (m => m.Something...), we'll push an empty
// string onto the stack and stop evaluating. The extra empty string makes sure that
// we don't accidentally cut off too much of m => m.Model.
nameParts.Push(string.Empty);
part = null;
}
else
{
break;
}
}
// If it starts with "model", then strip that away
if (nameParts.Count > 0 && string.Equals(nameParts.Peek(), ".model", StringComparison.OrdinalIgnoreCase))
{
nameParts.Pop();
}
if (nameParts.Count > 0)
{
return nameParts.Aggregate((left, right) => left + right).TrimStart('.');
}
return string.Empty;
}
private static string GetIndexerInvocation([NotNull] Expression expression,
[NotNull] ParameterExpression[] parameters)
{
var converted = Expression.Convert(expression, typeof(object));
var fakeParameter = Expression.Parameter(typeof(object), null);
var lambda = Expression.Lambda<Func<object, object>>(converted, fakeParameter);
Func<object, object> func;
try
{
func = CachedExpressionCompiler.Process(lambda);
}
catch (InvalidOperationException ex)
{
throw new InvalidOperationException(
Resources.FormatExpressionHelper_InvalidIndexerExpression(expression, parameters[0].Name),
ex);
}
return "[" + Convert.ToString(func(null), CultureInfo.InvariantCulture) + "]";
}
public static bool IsSingleArgumentIndexer(Expression expression)
{
var methodExpression = expression as MethodCallExpression;
if (methodExpression == null || methodExpression.Arguments.Count != 1)
{
return false;
}
// Check whether GetDefaultMembers() (if present in CoreCLR) would return a member of this type. Compiler
// names the indexer property, if any, in a generated [DefaultMember] attribute for the containing type.
var declaringType = methodExpression.Method.DeclaringType;
var defaultMember = declaringType.GetTypeInfo().GetCustomAttribute<DefaultMemberAttribute>(inherit: true);
if (defaultMember == null)
{
return false;
}
// Find default property (the indexer) and confirm its getter is the method in this expression.
return declaringType.GetRuntimeProperties().Any(
property => (string.Equals(defaultMember.MemberName, property.Name, StringComparison.Ordinal) &&
property.GetMethod == methodExpression.Method));
}
}
}