private TempComputeResult ComputeBinaryNative(Type leftType, Type rightType, object leftValue, object rightValue, string operatorText, ExpressionSyntax node) // 数学运算, 依赖动态编译lambda表达式,只能JIT { try { var func = new DynamicExpresso.Interpreter().Parse(string.Format("x{0}y", operatorText), new DynamicExpresso.Parameter("x", leftType.UnWrapper()), new DynamicExpresso.Parameter("y", rightType.UnWrapper())); var result = func.Invoke(leftValue, rightValue); return(CreateComputeResult(result, result == null ? null : result.GetType())); } catch (Exception e) { throw new Exception(string.Format("Fail to calculate '{0}'", node.ToString()), e); } }
public TempComputeResult VisitUnaryExpression(ExpressionSyntax operand, string operatorText, bool isPrefix, string nodeText) { var operandResult = Visit(operand); if (operandResult.Type == null) { return(CreateComputeResult(null, null)); } string methodName; if (dic_UnaryOperator_MethodName.TryGetValue(operatorText, out methodName)) { var bindingFlags = BindingFlags.Public | BindingFlags.Static; var overloadOperatorMethod = DebugService.GetMethod(operandResult.Type, methodName, bindingFlags, true, operandResult.Type); if (overloadOperatorMethod != null) // 有运算符重载 { var result = overloadOperatorMethod.Invoke(null, new object[1] { operandResult.Value }); return(CreateComputeResult(result, overloadOperatorMethod.ReturnType)); } } try { var expressionText = isPrefix ? "{0}x" : "x{0}"; var func = new DynamicExpresso.Interpreter().Parse(string.Format(expressionText, operatorText), new DynamicExpresso.Parameter("x", operandResult.Type.UnWrapper())); var result = func.Invoke(operandResult.Value); return(CreateComputeResult(result, result == null ? null : result.GetType())); } catch { throw new Exception(string.Format("Fail to calculate '{0}'", nodeText)); } }