コード例 #1
0
        public void MethodCallExpressionParse(MethodCallExpression methodCallExpression)
        {
            var methodName       = methodCallExpression.Method.Name;
            var methodCallObject = methodCallExpression.Object;

            //判断是常数调用方法,还是变量调用方法


            if (methodCallExpression.Method.DeclaringType == typeof(string))
            {
                var conventionMethodNameList = new List <string>()
                {
                    "Trim", "TrimEnd", "TrimStart", "ToUpper", "ToLower"
                };
                if (conventionMethodNameList.Contains(methodName))
                {
                    //转换后的方法名称
                    var afterConventionMethodName = "";
                    switch (methodCallExpression.Method.Name)
                    {
                    case "Trim":
                        afterConventionMethodName = "TRIM";
                        break;

                    case "TrimEnd":
                        afterConventionMethodName = "RTRIM";

                        break;

                    case "TrimStart":

                        afterConventionMethodName = "LTRIM";
                        break;

                    case "ToUpper":
                        afterConventionMethodName = "UPPER";
                        break;

                    case "ToLower":
                        afterConventionMethodName = "*";
                        break;
                    }

                    sqlInfo.AddSql(afterConventionMethodName + "(");
                    HandleExpressionByRecursion(methodCallObject);
                    sqlInfo.AddSql(")");
                }
                var likeMethodNameList = new List <string>()
                {
                    "Contains", "StartsWith", "EndsWith"
                };
                if (likeMethodNameList.Contains(methodName))
                {
                    var value  = "";
                    var argOne = methodCallExpression.Arguments[0];
                    switch (methodName)
                    {
                    case "Contains":
                        value = "%" + GetValue(argOne).ToString() + "%";
                        break;

                    case "StartsWith":
                        value = "" + GetValue(argOne).ToString() + "%";
                        break;

                    case "EndsWith":
                        value = "%" + GetValue(argOne).ToString();
                        break;
                    }
                    HandleExpressionByRecursion(methodCallObject);
                    sqlInfo.AddSql("like ");
                    sqlInfo.AddParameter(value);
                }

                if (methodName == "Equals")
                {
                    var argOne = methodCallExpression.Arguments[0];
                    HandleExpressionByRecursion(methodCallObject);
                    sqlInfo.AddSql("= ");
                    HandleExpressionByRecursion(argOne);
                }
            }

            //throw new Exception("Unsupported method call: " + expression.Method.Name);
        }