/// <summary> /// Parses the property. /// </summary> /// <returns>The property.</returns> /// <param name="current">Current.</param> /// <param name="toReturn">To return.</param> private static Expression ParseProperty(Expression current, ParsedExpression toReturn) { var me = (MemberExpression)current; toReturn.PrependProperty(me.Member.Name); current = me.Expression; return(current); }
/// <summary> /// Parse the specified propertyPath. /// </summary> /// <param name="propertyPath">Property path.</param> public static ParsedExpression Parse(LambdaExpression propertyPath) { var toReturn = new ParsedExpression(); var current = propertyPath.Body; while (current != null && current.NodeType != ExpressionType.Parameter) { current = ParseTo(current, toReturn); } return(toReturn); }
/// <summary> /// Parses the method call. /// </summary> /// <returns>The method call.</returns> /// <param name="current">Current.</param> /// <param name="toReturn">To return.</param> private static Expression ParseMethodCall(Expression current, ParsedExpression toReturn) { var me = (MethodCallExpression)current; if (me.Method.Name != "get_Item" || me.Arguments.Count != 1) { throw new ArgumentException( "Property expression must be of the form 'x => x.SomeProperty.SomeOtherProperty' or 'x => x.SomeCollection[0].Property'"); } var argument = me.Arguments[0]; argument = ConvertMemberAccessToConstant(argument); toReturn.PrependIndexed(argument.ToString()); current = me.Object; return(current); }
/// <summary> /// Parses to. /// </summary> /// <returns>The to.</returns> /// <param name="current">Current.</param> /// <param name="toReturn">To return.</param> private static Expression ParseTo(Expression current, ParsedExpression toReturn) { // This happens when a value type gets boxed if (current.NodeType == ExpressionType.Convert || current.NodeType == ExpressionType.ConvertChecked) { return(Unbox(current)); } if (current.NodeType == ExpressionType.MemberAccess) { return(ParseProperty(current, toReturn)); } if (current is MethodCallExpression) { return(ParseMethodCall(current, toReturn)); } throw new ArgumentException( "Property expression must be of the form 'x => x.SomeProperty.SomeOtherProperty'"); }