LambdaExpression解析(ExpressionVisitor)


Lambda表达式树解析主要通过ExpressionVisitor来实现,最终达到动态处理的效果:

ProductOrder po = new ProductOrder { OrderID = "2025", OrderDate = DateTime.Now };

            Expression> condition = (x, y) => x.OrderID == y.OrderID;

            
            
            if (condition is LambdaExpression)
            {
                if(condition.Body is BinaryExpression)
                {
                    BinaryExpression binaryExpression = condition.Body as BinaryExpression;
                    MemberExpression left = binaryExpression.Left as MemberExpression;
                    ParameterExpression leftParamExp = left.Expression as ParameterExpression;
                    //System.Linq.Expressions.ExpressionType

                    if (condition.Parameters[0].Name == leftParamExp.Name)
                    {
                        //leftParamExp
                        MemberInfo mi = left.Member;
                        PropertyInfo pi = po.GetType().GetProperty(mi.Name);
                        object ov = pi.GetValue(po);
                        //mi.get
                    }
                    //binaryExpression.Left
                }

                //BinaryExpression lambdaCondition = condition as BinaryExpression;
                //if(lambdaCondition.Parameters[0] == lambdaCondition. )
            }