Exemple #1
0
        internal Expression <T> BuildExpression <T>(string expressionName, string expression)
        {
            var owner     = DefaultExpressionOwner.Instance;
            var ownerType = DefaultExpressionOwner.Type;

            Imports.ImportOwner(ownerType);

            var context = new ExpressionContext(Options, expressionName, owner)
            {
                Variables       = Variables,
                Imports         = Imports,
                ComputeInstance = ComputeInstance,
            };

            var topElement = Parse(expression, context);

            var rootElement   = new RootExpressionElement(topElement, typeof(T));
            var dynamicMethod = CreateDynamicMethod <T>(ownerType);

            var ilGenerator = new YaleIlGenerator(dynamicMethod.GetILGenerator());

            rootElement.Emit(ilGenerator, context);

#if DEBUG
            ilGenerator.ValidateLength();
#endif

            var delegateType = typeof(ExpressionEvaluator <>).MakeGenericType(typeof(T));
            var evaluator    = (ExpressionEvaluator <T>)dynamicMethod.CreateDelegate(delegateType);

            return(new Expression <T>(expression, evaluator, context));
        }