public BoundExpression GetOrCreateBoundExpression(IBindingContext binder, BoundExpressionOptions options)
        {
            Require.NotNull(binder, "binder");
            Require.NotNull(options, "options");

            var key = new CacheKey(
                _dynamicExpression.ParseResult.Identifiers,
                !DynamicExpression.IsLanguageCaseSensitive(_dynamicExpression.Language),
                binder,
                options
                );

            lock (_syncRoot)
            {
                if (!_cache.TryGetValue(key, out var boundExpression))
                {
                    boundExpression = new BoundExpression(
                        _dynamicExpression,
                        key.OwnerType,
                        key.Imports,
                        key.IdentifierTypes,
                        key.Options
                        );

                    _cache.Add(key, boundExpression);
                }

                return(boundExpression);
            }
        }
Ejemplo n.º 2
0
        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));
        }