Exemplo n.º 1
0
        protected virtual object EvaluateExpression(string expression, object[] parameters = null,
                                                    bool skipRefinements = false)
        {
            var refinedExpression = expression;
            var classDefinition   = string.Empty;

            //0. initialize global usings
            var usings = new List <string>(UsedLibraries);

            //1. run refinements on input expression
            if (!skipRefinements)
            {
                foreach (ICodeRefiner refinement in Refinements)
                {
                    // use the original refinement if the author didnt implement clone correctly
                    var cloneRefinement = refinement.Clone() as ICodeRefiner ?? refinement;
                    refinedExpression = (cloneRefinement).RefineEvaluationString(refinedExpression);

                    if (cloneRefinement is UsingsExtractionRefinement)
                    {
                        var usingRefiner = cloneRefinement as UsingsExtractionRefinement;
                        usings.AddRange(usingRefiner.UsingStatements);
                    }
                }
            }

            //2. Build a class
            classDefinition = BuildClass(refinedExpression, usings);

            //3. Compile the class
            var assembly = CompileClass(classDefinition);

            //4. Run the code
            object retVal = null;

            if (assembly != null && assembly.CompiledAssembly != null)
            {
                retVal = ExecutionHelper.RunCode(assembly, parameters, OnRuntimeError);
            }

            return(retVal);
        }