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

Compile() public method

Compiles the input string and returns a delegate that represents the compiled code.
Compiles the input string as a C# expression or statement, unlike the Evaluate method, the resulting delegate can be invoked multiple times without incurring in the compilation overhead. 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 string was partial and that the invoking code should provide more code before the code can be successfully compiled. If you know that you will always get full expressions or statements and do not care about partial input, you can use the other Compile overload. On success, in addition to returning null, the compiled parameter will be set to the delegate that can be invoked to execute the code.
public Compile ( string input, CompiledMethod &compiled ) : string
input string
compiled CompiledMethod
return string
        public string Compile(string input, out CompiledMethod compiled)
        {
            if (input == null || input.Length == 0){
                compiled = null;
                return null;
            }

            lock (evaluator_lock){
                if (!inited)
                    Init ();
                else
                    ctx.Report.Printer.Reset ();

                bool partial_input;
                CSharpParser parser = ParseString (ParseMode.Silent, input, out partial_input);
                if (parser == null){
                    compiled = null;
                    if (partial_input)
                        return input;

                    ParseString (ParseMode.ReportErrors, input, out partial_input);
                    return null;
                }

                Class parser_result = parser.InteractiveResult;
                compiled = CompileBlock (parser_result, parser.undo, ctx.Report);
                return null;
            }
        }

Same methods

Evaluator::Compile ( string input ) : CompiledMethod

Usage Example

Example #1
0
    static void MonoCSharp_Test()
    {
        string className = GetClass();
        string code      = GetCode(className);

        evaluator.Compile(code);

        dynamic script = evaluator.Evaluate("new " + className + "();");

        int result = script.Sum(1, 2);
    }
All Usage Examples Of Mono.CSharp.Evaluator::Compile