public static Expression GetExpression(Variable variable, IDictionary <string, Expression> valuePairs, IDictionary <string, object> datas) { if (variable.Type == VariableType.StaticMethod) { InvokeVariable invokeVariable = (InvokeVariable)variable; return(BuildRealParam(invokeVariable, valuePairs, datas, null, invokeVariable.Path)); } Expression expression; ConstVariable constVariable = (ConstVariable)variable; if (!valuePairs.TryGetValue(constVariable.Name, out expression)) { if (constVariable.IsParamer == false) { expression = Expression.Constant(constVariable.GetValue(datas)); } else { expression = Expression.Parameter(constVariable.ValType, constVariable.Name); } valuePairs.Add(constVariable.Name, expression); } if (!constVariable.NotSelf) { return(expression); } return(BuildRealParam(constVariable, valuePairs, datas, expression, constVariable.Path)); }
public static Expression BuildRealParam(InvokeVariable variable, IDictionary <string, Expression> valuePairs, IDictionary <string, object> datas, Expression expression, string path, int start = 0) { var index = path.IndexOfAny(new char[] { Strings.Split, char.Parse(Strings.StartFlag1) }, start); string val = string.Empty; if (index < 0) { val = path.Substring(start); return(CallProperty(variable, expression, val)); } val = path.Substring(start, index - start); if (path[index] == Strings.Split) { expression = CallProperty(variable, expression, val); } else { return(CallMethod(variable, valuePairs, datas, expression, val)); } if (index > -1) { return(BuildRealParam(variable, valuePairs, datas, expression, path, index + 1)); } return(expression); }
public static Expression CallProperty(InvokeVariable variable, Expression expression, string propertyName) { if (variable.Type == VariableType.Const) { return(Expression.PropertyOrField(expression, propertyName)); } return(Expression.Field(null, ((StaticMethodVariable)variable).InstanceType, propertyName)); }
public static Expression CallMethod(InvokeVariable variable, IDictionary <string, Expression> valuePairs, IDictionary <string, object> datas, Expression expression, string methodName) { var varParams = variable.Params; MethodInfo methodInfo = null; List <Expression> paramExpressions = new List <Expression>(); Type[] types; if (varParams != null) { foreach (var param in varParams) { var fisrtParam = param.First(); if (param.Count() == 1) { paramExpressions.Add(GetExpression(fisrtParam, valuePairs, datas)); } else { var expressions = Util.BuildExpression(param, valuePairs, datas); paramExpressions.Add(expressions.First()); } } types = paramExpressions.Select(t => t.Type).ToArray(); } else { types = new Type[] { }; } if (variable.Type == VariableType.Const) { methodInfo = expression.Type.GetMethod(methodName, types); } else { methodInfo = ((StaticMethodVariable)variable).InstanceType.GetMethod(methodName, types); } return(Expression.Call(expression, methodInfo, paramExpressions.ToArray())); }
public static void SetInvokeParam(IEnumerable <Token> tokens, InvokeVariable variable, string flag, int i) { Token nextToken = null; if (tokens.Count() > (i + 1)) { nextToken = tokens.ElementAt(i + 1); } if (nextToken == null || nextToken.Flag != Strings.StartFlag1) { variable.Path = variable.Value.Remove(0, variable.Name.Count() + 1); return; } var result = GetParamVariables(tokens, flag, i); variable.Params = result.Item1; variable.Value = string.Concat(flag, Strings.StartFlag1, string.Join(string.Empty, result.Item2.Select(t => t.Flag)), Strings.EndFlag1); variable.Path = variable.Value.Remove(0, variable.Name.Count() + 1); variable.ParamTokenCount = result.Item2.Count() + 2; }