Example #1
0
 /// <summary>
 /// Adds a method to the dynamic class beeing generated.
 /// </summary>
 /// <param name="code"></param>
 /// <param name="item"></param>
 private static void AppendMethod(StringBuilder code, EvaluatorItem item)
 {
     code.AppendFormat("    public {0} {1}() ", item.ReturnType.Name, item.Name);
     code.Append("{ ");
     code.AppendFormat("      return ({0}); ", item.Expression);
     code.Append("}\n");
 }
Example #2
0
        /// <summary>
        /// Generates the code for the dynamic class to be compiled.
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private static String GetDynamicClassCode(EvaluatorItem item)
        {
            var code = new StringBuilder();

            AppendClassHeader(code);

            AppendMethod(code, item);

            code.Append("} }");

            return(code.ToString());
        }
Example #3
0
        /// <summary>
        /// Evaluates the givenn expression.
        /// </summary>
        /// <returns></returns>
        public static Object Evaluate(EvaluatorItem item)
        {
            var loSetup = new AppDomainSetup {
                ApplicationBase = AppDomain.CurrentDomain.BaseDirectory
            };
            var evaluationDomain = AppDomain.CreateDomain("ExpressionEvaluationContextDomainfda", null, loSetup);

            try
            {
                evaluationDomain.SetData("item", item);

                evaluationDomain.DoCallBack(EvaluateExpression);

                var result = evaluationDomain.GetData("result");

                return(result);
            }
            finally
            {
                AppDomain.Unload(evaluationDomain);
            }
        }