我正在使用一个.NET控制台应用程序,它在控制台中使用颜色和光标位置,我想将它的输出重定向到tcp套接字。从这个应用程序重定向文本很容易,我可以这样做没有问题,但我还需要重定向光标位置和颜色以获得完整的效果,据我所知,System.Console没有内置的方法可以做这一点。
但是,我知道如果我可以访问应用程序的源代码,我可以实现自己的类来模仿System.Console,这样我就可以替换任何Console实例。使用MyConsole。然后可以序列化所有控制台调用并将它们传递给tcp套接字或任何其他流。
所以基本上.NET应用程序有很多控制台调用,如:
<%@taglib prefix="sw" uri="/WEB-INF/sw.tld" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="menu" tagdir="/WEB-INF/
<div id="js-search-box" class="search-box">
<label>
<input id="div_id_${param.itemId}" type="text" placeholder=" <fmt:message key="search"/>">
<i class="fa fa-search"></i>
</label>
</div>
如果我有权访问源代码,我可以这样编写自己的类:
Console.WriteLine("blah");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.CursorLeft = 15;
,您明白了,然后用ConsoleWithTCP替换所有System.Console引用,即:
class ConsoleWithTCP
{
public const char CMD_INDICATOR = (char)01; //this is the ascii 'start of heading' character. I'll use it to denote the start and end of a console command
public const char CMD_END_INDICATOR = (char)02; //this is the ascii 'start of text' character. I'll use it to denote the end of a console command
public static void WriteLine(object obj) => WriteLine(obj.ToString());
public static void WriteLine(string format, params object[] args) => WriteLine(String.Format(format, args));
public static void WriteLine(string format, object arg0) => WriteLine(String.Format(format, arg0));
public static void WriteLine() => Write(Console.Out.NewLine);
public static void WriteLine(string line) => Write(line + Console.Out.NewLine);
public static void Write(string format, object arg0) => Write(String.Format(format, arg0));
public static void Write(string format, params object[] args) => Write(String.Format(format, args));
public static void Write(object obj) => Write(obj.ToString());
public static void Write(string content)
{
//escape any cmd_indicators
string cmdReplace = CMD_INDICATOR + "CMD_INDICATOR:" + CMD_END_INDICATOR;
content.Replace(CMD_INDICATOR.ToString(), cmdReplace);
//escape any cmd_end_indicators
cmdReplace = CMD_INDICATOR + "CMD_END_INDICATOR:" + CMD_END_INDICATOR;
content.Replace(CMD_END_INDICATOR.ToString(), cmdReplace);
Console.Write(content);
//also write to a TCP stream or a file
}
public static void WriteCmd(string cmdName, string data = "")
{
//make sure the command and data contain no special characters
if ( data.Contains(CMD_INDICATOR)
|| cmdName.Contains(CMD_INDICATOR)
|| data.Contains(CMD_END_INDICATOR)
|| cmdName.Contains(CMD_END_INDICATOR))
{
throw new InvalidDataException("The data string cannot contain an ascii character 01");
}
string cmd = CMD_INDICATOR + cmdName + ":" + data + CMD_END_INDICATOR;
//write the serialized command to a TCP stream or a file
}
public static void Clear()
{
Console.Clear();
WriteCmd("Clear");
}
public static ConsoleColor ForegroundColor
{
get { return Console.ForegroundColor; }
set
{
Console.ForegroundColor = value;
WriteCmd("ForegroundColor", value.ToString());
}
}
public static int CursorLeft
{
get { return Console.CursorLeft;}
set
{
Console.CursorLeft = value;
WriteCmd("CursorLeft", value.ToString());
}
}
}
有没有办法可以强制.NET为我执行此操作而无需修改源代码?