Bind Console Output to RichEdit

前端 未结 2 1622
北荒
北荒 2021-01-06 11:55

So pretty straightforward question. I have a c# .dll with a whole lot of Console.Writeline code and would like to be able to view that output in a forms application using th

2条回答
  •  -上瘾入骨i
    2021-01-06 12:13

    IMO, it would be better to refactor the existing code, replacing the existing Console.WriteLine to some central method in your code, and then repoint this method, presumably by supplying another TextWriter:

    private static TextWriter output = Console.Out;
    public static TextWriter Output {
       get {return output;}
       set {output = value ?? Console.Out;}
    }
    public static void WriteLine(string value) {
        output.WriteLine(value);
    }
    public static void WriteLine(string format, params string[] args) {
        output.WriteLine(format, args);
    }
    

    Or (simpler and less hacky re a static field), simply pass a TextWriter into your existing code and write to that?

提交回复
热议问题