/// <summary> /// 注入System.Console,以便在Console中使用颜色标记 /// </summary> /// <param name="foregroundColorTag">前景色标识字符</param> /// <param name="backgroundColorTag">背景色标识字符</param> /// <param name="maxColorDept">颜色区间嵌套级数</param> public static void Inject(char foregroundColorTag, char backgroundColorTag, int maxColorDept) { if (System.Threading.Volatile.Read(ref _isInjected)) { return; } System.Threading.Volatile.Write(ref _isInjected, true); TextWriter tw = new ColorTextWriter(_consoleOut) { BackgroundColorTag = backgroundColorTag, ForegroundColorTag = foregroundColorTag, MaxColorDept = maxColorDept }; tw = TextWriter.Synchronized(tw); if (!InjectByReflection(tw)) { System.Console.SetOut(tw); } }
static ColorConsole() { _consoleOut = System.Console.Out; _colorTW = new ColorTextWriter(_consoleOut); _colorOut = TextWriter.Synchronized(_colorTW); }