我需要一些帮助来简化这个包含数百行的巨大代码,我真的不知道该怎么做。 代码看起来非常混乱,我需要的是返回具有预定义文本颜色的模型。 有没有简单的方法?
我必须解释一下: - 有一个包含许多型号的手机列表,如果有这些型号的话,我需要打印出型号和颜色。该列表每小时/每天都在变化......这就是它的工作原理。 那么我需要用所有模型填写我的代码,然后搜索可用的代码。此外,所有型号“EndsWith”都是一个特定的代码(例如A23X),我认为这样搜索的速度更快。
if (text.EndsWith("model-1")) model.Add(new Line { text = "Sony, Smartphone(model-1)", color = () => Sony.Color1 });
if (text.EndsWith("model-2")) model.Add(new Line { text = "Sony, Smartphone(model-2)", color = () => Sony.Color2 });
if (text.EndsWith("model-3")) model.Add(new Line { text = "Sony, Smartphone(model-3)", color = () => Sony.Color3 });
if (text.EndsWith("model-4")) model.Add(new Line { text = "Sony, Smartphone(model-4)", color = () => Sony.Color4 });
if (text.EndsWith("model-5")) model.Add(new Line { text = "Sony, Smartphone(model-5)", color = () => Sony.Color5 });
if (text.EndsWith("model-6")) model.Add(new Line { text = "Sony, Smartphone(model-6)", color = () => Sony.Color6 });
if (text.EndsWith("model-7")) model.Add(new Line { text = "Sony, Smartphone(model-7)", color = () => Sony.Color7 });
if (text.EndsWith("model-8")) model.Add(new Line { text = "Sony, Smartphone(model-8)", color = () => Sony.Color8 });
if (text.EndsWith("model-9")) model.Add(new Line { text = "Sony, Smartphone(model-9)", color = () => Sony.Color9 });
if (text.EndsWith("model-10")) model.Add(new Line { text = "Nokia, Smartphone(model-10)", color = () => Nokia.Color10 });
if (text.EndsWith("model-11")) model.Add(new Line { text = "Nokia, Smartphone(model-11)", color = () => Nokia.Color11 });
if (text.EndsWith("model-12")) model.Add(new Line { text = "Nokia, Smartphone(model-12)", color = () => Nokia.Color12 });
if (text.EndsWith("model-13")) model.Add(new Line { text = "Nokia, Smartphone(model-13)", color = () => Nokia.Color13 });
if (text.EndsWith("model-14")) model.Add(new Line { text = "Nokia, Smartphone(model-14)", color = () => Nokia.Color14 });
if (text.EndsWith("model-15")) model.Add(new Line { text = "Nokia, Smartphone(model-15)", color = () => Nokia.Color15 });
if (text.EndsWith("model-16")) model.Add(new Line { text = "Nokia, Smartphone(model-16)", color = () => Nokia.Color16 });
if (text.EndsWith("model-17")) model.Add(new Line { text = "Nokia, Smartphone(model-17)", color = () => Nokia.Color17 });
if (text.EndsWith("model-18")) model.Add(new Line { text = "Nokia, Smartphone(model-18)", color = () => Nokia.Color18 });
答案 0 :(得分:7)
您可以创建这样的字典,以便从模型名称中查找Line
实例:
Dictionary<string, Line> ModelNames = new Dictionary<string, Line>
{
{"model-1", new Line { text = "Sony, Smartphone(model-1)", color = Sony.Color1 }},
{"model-2", new Line { text = "Sony, Smartphone(model-2)", color = Sony.Color2 }},
{"model-3", new Line { text = "Sony, Smartphone(model-3)", color = Sony.Color3 }},
// ...
{"model-10", new Line { text = "Nokia, Smartphone(model-10)", color = Nokia.Color10 }},
{"model-11", new Line { text = "Nokia, Smartphone(model-11)", color = Nokia.Color11 }},
{"model-12", new Line { text = "Nokia, Smartphone(model-12)", color = Nokia.Color12 }},
// ...
};
现在你可以看看是否有一个已知模型:
Line matchingModelNameLine = ModelNames
.Where(kv => text.EndsWith(kv.Key, StringComparison.InvariantCultureIgnoreCase))
.Select(kv => kv.Value)
.FirstOrDefault();
if (matchingModelNameLine != null)
{
model.Add(matchingModelNameLine);
}
答案 1 :(得分:3)
我不知道,也许这样试试。
Regex model = new Regex(@"?<=model-)\d+");
int model;
String description;
object sColor; //Take the proper type here, no object
if (model.IsMatch("") && int.TryParse(model.Match("").Value, out model)) {
if (model > 0 && model < 10) {
description = String.Format("Sony, Smartphone(model-{0})", model);
sColor = Enum.Parse(typeof(Sony), String.Format("Color{0}", model))
}
else if (model > 9 && model < 19) {
description = String.Format("Nokia, Smartphone(model-{0})", model);
sColor = Enum.Parse(typeof(Nokia), String.Format("Color{0}", model))
}
model.Add(new Line { text = description, color = sColor });
}
答案 2 :(得分:2)
在这种情况下我通常做的是匹配词典 - &gt;操作。首先提取每次迭代更改的数据并为其创建一个类,例如:
class OperationData{
public string Manufacturer { get; set;}
public ColorEnum Color { get; set;} //some way to persist the color, might be enum value, stringified enum name, parent class...
}
现在使用执行操作所需的参数映射所有后缀(我假设为OperationData的构造函数):
var operationMap = new Dictionary<string, OperationData>();
//now add all the definitions
oprationMap.Add("model-10", new OperationData("Nokia", Nokia.Color10));
//.....
并处理数据(foreach是为了简单,但LINQ将是我的方式):
foreach(var row in operationMap){
var key = row.Key;
var data = row.Value;
if (text.EndsWith(key)) {
model.Add(new Line {
text = String.Format("{0}, Smartphone({1})", data.Manufacturer, key),
color = () => data.Color
});
break;
}
}
此类解决方案的最大好处是,如果您需要更改逻辑,则可以一次性完成。您可以轻松修改匹配字符串或字符串格式的方式,甚至可以修改您创建的Line类。您也可以设想将其重用于不同的数据集。