public static bool Evaluate(string questionCode, string displayLogic, string response) { var isInverse = displayLogic.Contains("!="); displayLogic = displayLogic.Replace(questionCode, "a"); if (isInverse) { displayLogic = displayLogic.Replace("!=", "=="); } var expression = new DynamicExpression(displayLogic, ExpressionLanguage.Csharp); var context = new ExpressionContext(); context.Variables.Add("a", response); var result = (bool)expression.Invoke(context); return isInverse ? !result : result; }
public object Invoke(IExecutionContext executionContext) { if (executionContext == null) executionContext = new ExpressionContext(); var parameters = new object[_parameterMap.Length]; bool ignoreCase = !DynamicExpression.IsLanguageCaseSensitive(_dynamicExpression.Language); var identifiers = _dynamicExpression.ParseResult.Identifiers; for (int i = 0; i < parameters.Length; i++) { int index = _parameterMap[i]; if (index == -1) { parameters[i] = executionContext.Owner; } else { parameters[i] = executionContext.GetVariableValue( identifiers[index].Name, ignoreCase ); } } return _compiledMethod(parameters); }
/// <summary> /// Invokes the expression with the specified expression context and /// binding options. /// </summary> /// <param name="expressionContext">The expression context used to /// bind and execute the expression.</param> /// <param name="options">The options used to bind the expression.</param> /// <returns>The result of the expression.</returns> public object Invoke(IExpressionContext expressionContext, BoundExpressionOptions options) { if (expressionContext == null) expressionContext = new ExpressionContext(); return Bind(expressionContext, options).Invoke(expressionContext); }
/// <summary> /// Binds the compiled expression with the specified binding context and options. /// </summary> /// <param name="binder">The binding context used to bind the expression.</param> /// <param name="options">The options used to bind the expression.</param> /// <returns>The bound expression.</returns> public IBoundExpression Bind(IBindingContext binder, BoundExpressionOptions options) { if (binder == null) binder = new ExpressionContext(); if (options == null) options = new BoundExpressionOptions(); options.Freeze(); return Cached.GetOrCreateBoundExpression(binder, options); }
public object Invoke(IExecutionContext executionContext) { bool hadExecutionContext = executionContext != null; if (executionContext == null) executionContext = new ExpressionContext(); var parameters = new object[_parameterMap.Length]; bool ignoreCase = !DynamicExpression.IsLanguageCaseSensitive(_dynamicExpression.Language); var identifiers = _dynamicExpression.ParseResult.Identifiers; for (int i = 0; i < parameters.Length; i++) { int index = _parameterMap[i]; if (index == -1) { if (!hadExecutionContext) { throw new ExpressionsException( "An owner was expected but no execution context has been provided", ExpressionsExceptionType.InvalidOperation ); } parameters[i] = executionContext.Owner; } else { parameters[i] = executionContext.GetVariableValue( identifiers[index].Name, ignoreCase ); } } return _compiledMethod(parameters); }