1 namespace System.Web.Mvc {
3 using System.Collections.Generic;
4 using System.Globalization;
6 using System.Linq.Expressions;
7 using System.Reflection;
8 using System.Web.Mvc.Resources;
10 public static class ExpressionHelper {
11 public static string GetExpressionText(string expression) {
13 String.Equals(expression, "model", StringComparison.OrdinalIgnoreCase)
14 ? String.Empty // If it's exactly "model", then give them an empty string, to replicate the lambda behavior
18 public static string GetExpressionText(LambdaExpression expression) {
19 // Split apart the expression string for property/field accessors to create its name
20 Stack<string> nameParts = new Stack<string>();
21 Expression part = expression.Body;
23 while (part != null) {
24 if (part.NodeType == ExpressionType.Call) {
25 MethodCallExpression methodExpression = (MethodCallExpression)part;
27 if (!IsSingleArgumentIndexer(methodExpression)) {
33 methodExpression.Arguments.Single(),
34 expression.Parameters.ToArray()
38 part = methodExpression.Object;
40 else if (part.NodeType == ExpressionType.ArrayIndex) {
41 BinaryExpression binaryExpression = (BinaryExpression)part;
45 binaryExpression.Right,
46 expression.Parameters.ToArray()
50 part = binaryExpression.Left;
52 else if (part.NodeType == ExpressionType.MemberAccess) {
53 MemberExpression memberExpressionPart = (MemberExpression)part;
54 nameParts.Push("." + memberExpressionPart.Member.Name);
55 part = memberExpressionPart.Expression;
57 else if (part.NodeType == ExpressionType.Parameter) {
59 // When the expression is parameter based (m => m.Something...), we'll push an empty
60 // string onto the stack and stop evaluating. The extra empty string makes sure that
61 // we don't accidentally cut off too much of m => m.Model.
62 nameParts.Push(String.Empty);
70 // If it starts with "model", then strip that away
71 if (nameParts.Count > 0 && String.Equals(nameParts.Peek(), ".model", StringComparison.OrdinalIgnoreCase)) {
75 if (nameParts.Count > 0) {
76 return nameParts.Aggregate((left, right) => left + right).TrimStart('.');
82 private static string GetIndexerInvocation(Expression expression, ParameterExpression[] parameters) {
83 Expression converted = Expression.Convert(expression, typeof(object));
84 ParameterExpression fakeParameter = Expression.Parameter(typeof(object), null);
85 Expression<Func<object, object>> lambda = Expression.Lambda<Func<object, object>>(converted, fakeParameter);
86 Func<object, object> func;
89 func = ExpressionUtil.CachedExpressionCompiler.Process(lambda);
91 catch (InvalidOperationException ex) {
92 throw new InvalidOperationException(
94 CultureInfo.CurrentCulture,
95 MvcResources.ExpressionHelper_InvalidIndexerExpression,
103 return "[" + Convert.ToString(func(null), CultureInfo.InvariantCulture) + "]";
106 internal static bool IsSingleArgumentIndexer(Expression expression) {
107 MethodCallExpression methodExpression = expression as MethodCallExpression;
108 if (methodExpression == null || methodExpression.Arguments.Count != 1) {
112 return methodExpression.Method
115 .OfType<PropertyInfo>()
116 .Any(p => p.GetGetMethod() == methodExpression.Method);