How to use ErrorListener for IronRuby

后端 未结 2 675
星月不相逢
星月不相逢 2021-01-22 13:35

I have a C# program to execute an IronRuby script. But before doing that, I\'d like to compile the file first to see if there is any errors. But it seems the ErrorListener does

2条回答
  •  孤街浪徒
    2021-01-22 13:53

    What you're trying to do seems to not actually work. Not sure if it's a bug or not, though.

    To workaround it, just execute the code inside a try/catch block and look for MissingMethodExecption. Pay attention that this also will not help if the syntax error is inside a method since IronRuby (or any other dynamic language) doesn't do anything with "nested" code until it actually executes it.

    So in general, I think you won't get a lot of value from what you're trying to do.

    The try/catch code sample:

    ScriptEngine engine = null;
    engine = Ruby.CreateEngine(x => { x.ExceptionDetail = true; });         
    
    ScriptSource sc = engine.CreateScriptSourceFromFile("MainForm.rb");
    ErrorListener errLis = new MyErrorListener();
    sc.Compile(errLis);
    
    try
    {
        dynamic d = sc.Execute();
    }
    catch (MissingMethodException e)
    {
        Console.WriteLine("Syntax error!");
    }
    

提交回复
热议问题