Mono Evaluator Class

I am using the Mono Evaluator class to run C # scripts. If there is a syntax error in the code, the error is displayed on the console. I would prefer the result to return to String. I know that I can redirect the entire console, but I would rather just get the result of the analyzer.

There is a MessageOutput property, which is a TextWriter, but I have no idea what to do with it.

+3
source share
2 answers

Just gonna guess here ...

Create a new one System.IO.StringWriterand assign it MessageOutput?

If it works, you can get the contents of StringWriter through ToString().

+1
source

, , . , ( "", ) var lastOutput.

:

1) ConsoleReportPrinter CustomTextWriter, Write/WriteLine
 2) ReportPrinter Evaluator CompilerContext

class CustomTextWriter : TextWriter
{
    private string lastOutput { get; set; }
    public CustomTextWriter() { }
    public override void Write(string value)
    {
        lastOutput = value;
        Console.Write(value);
    }
    public override void WriteLine(string value)
    {
        lastOutput = value;
        Console.WriteLine(value);
    }
    public override Encoding Encoding
    {
        get
        {
            return Encoding.Default;
        }
    }
}
static void Main(string[] args)
{
    ReportPrinter r = new ConsoleReportPrinter(new CustomTextWriter());
    evaluator = new Evaluator(new CompilerContext(
                        new CompilerSettings(),
                        r));
    // all evaluations now will pass through our CustomTextWriter
}
0

Source: https://habr.com/ru/post/1783998/


All Articles