Mono.CSharp.Evaluator.Evaluate C# (CSharp) Method

Evaluate() public method

Evaluates and expression or statement and returns any result values.
Evaluates the input string as a C# expression or statement. If the input string is an expression the result will be stored in the result variable and the result_set variable will be set to true. It is necessary to use the result/result_set pair to identify when a result was set (for example, execution of user-provided input can be an expression, a statement or others, and result_set would only be set if the input was an expression. If the return value of this function is null, this indicates that the parsing was complete. If the return value is a string, it indicates that the input is partial and that the user should provide an updated string.
public Evaluate ( string input, object &result, bool &result_set ) : string
input string
result object
result_set bool
return string
        public string Evaluate(string input, out object result, out bool result_set)
        {
            CompiledMethod compiled;

            result_set = false;
            result = null;

            input = Compile (input, out compiled);
            if (input != null)
                return input;

            if (compiled == null)
                return null;

            //
            // The code execution does not need to keep the compiler lock
            //
            object retval = typeof (QuitValue);

            try {
                invoke_thread = System.Threading.Thread.CurrentThread;
                invoking = true;
                compiled (ref retval);
            } catch (ThreadAbortException e){
                //Thread.ResetAbort ();
                Console.WriteLine ("Interrupted!\n{0}", e);
            } finally {
                invoking = false;
            }

            //
            // We use a reference to a compiler type, in this case
            // Driver as a flag to indicate that this was a statement
            //
            if (!ReferenceEquals (retval, typeof (QuitValue))) {
                result_set = true;
                result = retval;
            }

            return null;
        }

Same methods

Evaluator::Evaluate ( string input ) : object

Usage Example

Example #1
0
        public Task <bool> EvaluateExpression(string expression, string code, EvalResult result)
        {
            EnsureConfigured();
            try
            {
                object retResult;
                bool   hasResult;

                printer.Reset();
                if (!String.IsNullOrEmpty(code))
                {
                    var ret = eval.Evaluate(code, out retResult, out hasResult);
                }
                result.Result = eval.Evaluate(expression);
                return(Task.FromResult(true));
            }
            catch (Exception ex)
            {
                Log.Error($"Error creating a new instance of {expression}");
                if (printer.Messages.Count != 0)
                {
                    result.Messages = printer.Messages.ToArray();
                }
                else
                {
                    result.Messages = new EvalMessage[] { new EvalMessage("error", ex.ToString()) };
                }
                if (!result.HasResult && result.Messages.Length == 0)
                {
                    result.Messages = new EvalMessage[] { new EvalMessage("error", "Internal Error") };
                }
                eval = null;
            }
            return(Task.FromResult(false));
        }
All Usage Examples Of Mono.CSharp.Evaluator::Evaluate