0
我現在有一個switch語句,看起來很笨重。它可以工作,但它非常笨重和重複性,因爲我實際上有另一個開關語句(它需要一個不同的變量)。是否有重構此switch語句的好方法?
if (time == 15)
{
percent = policyGroup[0];
}
else
{
percent = policyGroup[1];
}
var currentVal = Convert.ToDouble(profit[i]);
switch (CSR[i])
{
case "Buford":
Buford += currentVal * percent;
break;
case "Cain":
Cain += currentVal * percent;
break;
case "Young":
Young += currentVal * percent;
break;
case "Thomas":
Thomas += currentVal * percent;
break;
case "Armendt":
Armendt += currentVal * percent;
break;
case "Vanover":
Vanover += currentVal * percent;
break;
case "Phillips":
Phillips += currentVal * percent;
break;
case "Smalley":
Smalley += currentVal * percent;
break;
case "Pitman":
Pitman += currentVal * percent;
break;
case "Huse":
Huse += currentVal * percent;
break;
case "Nichols":
Nichols += currentVal * percent;
break;
}
字典或使用反射.... –
[重構的可能的複製長開關語句](http://stackoverflow.com/questions/20586041/refactor-long-switch-statement) – Usman
如果這些變量是不可變的類型,那麼你只能用反射 – Steve