/// <summary>
        /// extracts values from equality expressions that match parameter names
        /// </summary>
        /// <param name="be">binary expression to evaluate</param>
        /// <returns>binary expression - supports recursive tree traversal in visitor</returns>
        protected override Expression VisitBinary(BinaryExpression be)
        {
            if (be.NodeType == ExpressionType.Equal ||
                be.NodeType == ExpressionType.GreaterThan ||
                be.NodeType == ExpressionType.GreaterThanOrEqual ||
                be.NodeType == ExpressionType.LessThan ||
                be.NodeType == ExpressionType.LessThanOrEqual ||
                be.NodeType == ExpressionType.NotEqual)
            {
                foreach (var param in ParameterNames)
                {
                    if (ExpressionTreeHelpers.IsMemberEqualsValueExpression(be, typeof(T), param))
                    {
                        parameters.Add(param, ExpressionTreeHelpers.GetValueFromEqualsExpression(be, typeof(T), param));
                        return(be);
                    }
                }

                return(base.VisitBinary(be));
            }
            else
            {
                return(base.VisitBinary(be));
            }
        }
Example #2
0
        /// <summary>
        /// test to see if expression is a binary expression that checks equality with a constant value
        /// - essentially, the caller wants to know if this is a well-formed expression with certain criteria
        /// </summary>
        /// <param name="exp">expression to check</param>
        /// <param name="declaringType">type containing member</param>
        /// <param name="memberName">member being checked</param>
        /// <returns>true if member is being checked for equality with value</returns>
        internal static bool IsMemberEqualsValueExpression(Expression exp, Type declaringType, string memberName)
        {
            if (exp.NodeType != ExpressionType.Equal)
            {
                return(false);
            }

            BinaryExpression be = (BinaryExpression)exp;

            // Assert.
            if (ExpressionTreeHelpers.IsSpecificMemberExpression(be.Left, declaringType, memberName) &&
                ExpressionTreeHelpers.IsSpecificMemberExpression(be.Right, declaringType, memberName))
            {
                throw new Exception("Cannot have 'member' == 'member' in an expression!");
            }

            return(ExpressionTreeHelpers.IsSpecificMemberExpression(be.Left, declaringType, memberName) ||
                   ExpressionTreeHelpers.IsSpecificMemberExpression(be.Right, declaringType, memberName));
        }