Gotos的Mono CSharp编译错误

时间:2013-11-20 22:41:29

标签: c# mono goto

我正在使用Mono.CSharp.Evaluator的一个实例编译一些代码并返回一个函数。在我使用goto之前,它一直没有问题。我正在使用VS2012构建.NET 4.5。我通过Evaluator.Evaluate运行以下代码,并将其存储在一个对象中以供以后执行:

        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;

        });

我收到InternalErrorException((1,1):) InnerException是

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()

我正在设置Evaluator(_e)

        _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;");

有人有任何想法如何解决这个问题吗?

谢谢, 本

1 个答案:

答案 0 :(得分:0)

经过一些摆弄,我通过更改为Evaluator.Run,​​稍微更改运行代码,然后运行Evaluator.Evaluate来修复此问题。修改后的代码

_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");