Question

I am using an instance of Mono.CSharp.Evaluator to compile some code and return a function. It has worked without issue until I used a goto. I am building for .NET 4.5 with VS2012. I am running the following code through Evaluator.Evaluate, and storing it in an object for later execution:

        Func<Dictionary<string, object>, dynamic, LogWrapperMethod, LogWrapperMethod, LogWrapperMethod, LogWrapperMethod, ExcWrapperMethod, AddResultWrapperMethod, int> a = new Func<Dictionary<string, object>, dynamic, LogWrapperMethod, LogWrapperMethod, LogWrapperMethod, LogWrapperMethod, ExcWrapperMethod, AddResultWrapperMethod, int>((parameters, self, debug, log, warn, error, exception, addResult) =>
        {

            Console.WriteLine("beforegoto");
            goto Ben;
        Ben:
            Console.WriteLine("gotoResult");
            return 0;

        });

I am getting InternalErrorException ((1,1): ) The InnerException is

Bad label content in ILGenerator

at System.Reflection.Emit.ILGenerator.GetLabelPos(Label lbl)
at System.Reflection.Emit.ILGenerator.BakeByteArray()
at System.Reflection.Emit.MethodBuilder.CreateMethodBodyHelper(ILGenerator il)
at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
at System.Reflection.Emit.TypeBuilder.CreateType()
at Mono.CSharp.TypeDefinition.CloseContainer()

I am setting up the Evaluator (_e) as such

        _settings = new CompilerSettings
                        {
                            EnhancedWarnings = true,
                            Stacktrace = true,
                            ShowFullPaths = true,
                            Timestamps = true,
                            Optimize = true,
                            AssemblyReferences = new List<string>
                                                     {
                                                         "Microsoft.CSharp.dll"
                                                     },
                        };
        _ctx = new CompilerContext(_settings, new Reporter());
        _e = new Evaluator(_ctx);
        _e.Run("using System;");
        _e.Run("using System.Collections.Generic;");
        _e.Run("using System.Dynamic;");
        _e.Run("using System.Linq;");
        _e.Run("using System.Text.RegularExpressions;");

Does anybody have any ideas how to resolve this issue?

Thanks, Ben

Was it helpful?

Solution

After some fiddling, I fixed this issue by changing to Evaluator.Run, changing the run code slightly, and then running Evaluator.Evaluate. Revised code below

_e.Run("object o = Func<Dictionary<string, object>, dynamic, LogWrapperMethod, LogWrapperMethod, LogWrapperMethod, LogWrapperMethod, ExcWrapperMethod, AddResultWrapperMethod, int> a = new Func<Dictionary<string, object>, dynamic, LogWrapperMethod, LogWrapperMethod, LogWrapperMethod, LogWrapperMethod, ExcWrapperMethod, AddResultWrapperMethod, int>((parameters, self, debug, log, warn, error, exception, addResult) =>
    {

        Console.WriteLine(\"beforegoto\");
        goto Ben;
    Ben:
        Console.WriteLine(\"gotoResult\");
        return 0;

    });");
object func = _e.Evaluate("o");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top