1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\r
5 * This software is subject to the Microsoft Public License (Ms-PL).
\r
6 * A copy of the license can be found in the license.htm file included
\r
7 * in this distribution.
\r
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
15 using System.Collections.Generic;
\r
16 using System.Globalization;
\r
18 using System.Linq.Expressions;
\r
19 using System.Reflection;
\r
20 using System.Web.Mvc.Resources;
\r
22 public static class ExpressionHelper {
\r
23 public static string GetExpressionText(string expression) {
\r
25 String.Equals(expression, "model", StringComparison.OrdinalIgnoreCase)
\r
26 ? String.Empty // If it's exactly "model", then give them an empty string, to replicate the lambda behavior
\r
30 public static string GetExpressionText(LambdaExpression expression) {
\r
31 // Crack the expression string for property/field accessors to create its name
\r
32 Stack<string> nameParts = new Stack<string>();
\r
33 Expression part = expression.Body;
\r
35 while (part != null) {
\r
36 if (part.NodeType == ExpressionType.Call) {
\r
37 MethodCallExpression methodExpression = (MethodCallExpression)part;
\r
39 if (!IsSingleArgumentIndexer(methodExpression)) {
\r
44 GetIndexerInvocation(
\r
45 methodExpression.Arguments.Single(),
\r
46 expression.Parameters.ToArray()
\r
50 part = methodExpression.Object;
\r
52 else if (part.NodeType == ExpressionType.ArrayIndex) {
\r
53 BinaryExpression binaryExpression = (BinaryExpression)part;
\r
56 GetIndexerInvocation(
\r
57 binaryExpression.Right,
\r
58 expression.Parameters.ToArray()
\r
62 part = binaryExpression.Left;
\r
64 else if (part.NodeType == ExpressionType.MemberAccess) {
\r
65 MemberExpression memberExpressionPart = (MemberExpression)part;
\r
66 nameParts.Push("." + memberExpressionPart.Member.Name);
\r
67 part = memberExpressionPart.Expression;
\r
74 // If it starts with "model", then strip that away
\r
75 if (nameParts.Count > 0 && String.Equals(nameParts.Peek(), ".model", StringComparison.OrdinalIgnoreCase)) {
\r
79 if (nameParts.Count > 0) {
\r
80 return nameParts.Aggregate((left, right) => left + right).TrimStart('.');
\r
83 return String.Empty;
\r
86 private static string GetIndexerInvocation(Expression expression, ParameterExpression[] parameters) {
\r
87 Expression converted = Expression.Convert(expression, typeof(object));
\r
88 ParameterExpression fakeParameter = Expression.Parameter(typeof(object), null);
\r
89 Expression<Func<object, object>> lambda = Expression.Lambda<Func<object, object>>(converted, fakeParameter);
\r
90 Func<object, object> func;
\r
93 func = ExpressionUtil.CachedExpressionCompiler.Process(lambda);
\r
95 catch (InvalidOperationException ex) {
\r
96 throw new InvalidOperationException(
\r
98 CultureInfo.CurrentUICulture,
\r
99 MvcResources.ExpressionHelper_InvalidIndexerExpression,
\r
107 return "[" + Convert.ToString(func(null), CultureInfo.InvariantCulture) + "]";
\r
110 internal static bool IsSingleArgumentIndexer(Expression expression) {
\r
111 MethodCallExpression methodExpression = expression as MethodCallExpression;
\r
112 if (methodExpression == null || methodExpression.Arguments.Count != 1) {
\r
116 return methodExpression.Method
\r
118 .GetDefaultMembers()
\r
119 .OfType<PropertyInfo>()
\r
120 .Any(p => p.GetGetMethod() == methodExpression.Method);
\r