问题
I want my ConsoleEngine class to handle the Console.WriteLine() Method. How do I have to prepare my ConsoleEngine class to override the WriteLine() method?
回答1:
You can create a class that derives from TextWriter:
public class MyWriter : TextWriter
{
public override void Write(char value)
{
//Do something, like write to a file or something
}
public override void Write(string value)
{
//Do something, like write to a file or something
}
public override Encoding Encoding
{
get
{
return Encoding.ASCII;
}
}
}
and set the Console output to an instance of that class:
Console.SetOut(new MyWriter());
来源:https://stackoverflow.com/questions/32661000/how-can-i-use-an-own-class-for-console-writeline-override