用循环重构代码? [英] Refactor the code with loop?
本文介绍了用循环重构代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个方法。
public void AddForegroundColor(ColoredConsoleTarget coloredConsoleTarget )
{
var consoleColors = _loggerModel.console_color;
列表< ConsoleOutputColor> consoleOutputColorList = new 列表< ConsoleOutputColor>();
ConsoleOutputColor traceForegroundColor;
ConsoleOutputColor infoForegroundColor;
ConsoleOutputColor debugForegroundColor;
ConsoleOutputColor warnForegroundColor;
ConsoleOutputColor errorForegroundColor;
ConsoleOutputColor fatalForegroundColor;
ConsoleOutputColor.TryParse(consoleColors.TRACE, true , out traceForegroundColor);
ConsoleOutputColor.TryParse(consoleColors.INFO, true , out infoForegroundColor);
ConsoleOutputColor.TryParse(consoleColors.DEBUG, true , out debugForegroundColor);
ConsoleOutputColor.TryParse(consoleColors.WARN, true , out warnForegroundColor);
ConsoleOutputColor.TryParse(consoleColors.ERROR, true , out errorForegroundColor);
ConsoleOutputColor.TryParse(consoleColors.FATAL, true , out fatalForegroundColor);
consoleOutputColorList.Add(traceForegroundColor);
consoleOutputColorList.Add(infoForegroundColor);
consoleOutputColorList.Add(debugForegroundColor);
consoleOutputColorList.Add(warnForegroundColor);
consoleOutputColorList.Add(errorForegroundColor);
consoleOutputColorList.Add(fatalForegroundColor);
AddConsoleRowHighlightingRule(coloredConsoleTarget,consoleOutputColorList);
}
我觉得它很脏,我们能清理它吗?我想使用for循环左右?
解决方案
假设consoleColors
项的类型为ConsoleColorType
,可以使用类似的东西:
public void AddForegroundColor(ColoredConsoleTarget coloredConsoleTarget)
{
var consoleColors = _loggerModel.console_color;
var colorTypes = new ConsoleColorType []
{
consoleColors。 TRACE,
consoleColors.INFO,
consoleColors.DEBUG,
consoleColors.WARN,
consoleColors.ERROR,
consoleColors.FATAL,
};
var consoleOutputColorList = new 列表< consoleoutputcolor>() ;
foreach ( var item in colorTypes)
{
ConsoleOutputColor traceColor;
ConsoleOutputColor.TryParse(item, true , out traceColor);
consoleOutputColorList.Add(traceColor);
}
AddConsoleRowHighlightingRule(coloredConsoleTarget,consoleOutputColorList);
}
根据颜色类型进行必要的调整。
更新:由于使用了字符串,因此以下行
var colorTypes = new ConsoleColorType []
应该更改为
var colorTypes = new string []
I have a method.
public void AddForegroundColor(ColoredConsoleTarget coloredConsoleTarget)
{
var consoleColors = _loggerModel.console_color;
List<ConsoleOutputColor> consoleOutputColorList = new List<ConsoleOutputColor>();
ConsoleOutputColor traceForegroundColor;
ConsoleOutputColor infoForegroundColor;
ConsoleOutputColor debugForegroundColor;
ConsoleOutputColor warnForegroundColor;
ConsoleOutputColor errorForegroundColor;
ConsoleOutputColor fatalForegroundColor;
ConsoleOutputColor.TryParse(consoleColors.TRACE, true, out traceForegroundColor);
ConsoleOutputColor.TryParse(consoleColors.INFO, true, out infoForegroundColor);
ConsoleOutputColor.TryParse(consoleColors.DEBUG, true, out debugForegroundColor);
ConsoleOutputColor.TryParse(consoleColors.WARN, true, out warnForegroundColor);
ConsoleOutputColor.TryParse(consoleColors.ERROR, true, out errorForegroundColor);
ConsoleOutputColor.TryParse(consoleColors.FATAL, true, out fatalForegroundColor);
consoleOutputColorList.Add(traceForegroundColor);
consoleOutputColorList.Add(infoForegroundColor);
consoleOutputColorList.Add(debugForegroundColor);
consoleOutputColorList.Add(warnForegroundColor);
consoleOutputColorList.Add(errorForegroundColor);
consoleOutputColorList.Add(fatalForegroundColor);
AddConsoleRowHighlightingRule(coloredConsoleTarget, consoleOutputColorList);
}
I felt it is dirty, can we clean it? I guess using for loop or so?
解决方案
Assuming thatconsoleColors
item are of typeConsoleColorType
, something like that could be used:
public void AddForegroundColor(ColoredConsoleTarget coloredConsoleTarget) { var consoleColors = _loggerModel.console_color; var colorTypes = new ConsoleColorType[] { consoleColors.TRACE, consoleColors.INFO, consoleColors.DEBUG, consoleColors.WARN, consoleColors.ERROR, consoleColors.FATAL, }; var consoleOutputColorList = new List<consoleoutputcolor>(); foreach (var item in colorTypes) { ConsoleOutputColor traceColor; ConsoleOutputColor.TryParse(item, true, out traceColor); consoleOutputColorList.Add(traceColor); } AddConsoleRowHighlightingRule(coloredConsoleTarget, consoleOutputColorList); }
Make any necessary adjustment depending on color types.
Update: Since strings are used, the following line
var colorTypes = new ConsoleColorType[]
should be change for
var colorTypes = new string[]
这篇关于用循环重构代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文