我相信这个解决方案是最直接的,你绝对应该使用它:
switch(a) {
case 1: return "lalala";
case 2: return "blabla";
case 3: return "lololo";
default: return "default";
}
但是,既然你要求一个return
,你可以使用这个流利的小类:
public class Switch<TElement, TResult> {
TElement _element;
TElement _currentCase;
IDictionary<TElement, TResult> _map = new Dictionary<TElement, TResult>();
public Switch(TElement element) { _element = element; }
public Switch<TElement, TResult> Case(TElement element) {
_currentCase = element;
return this;
}
public Switch<TElement, TResult> Then(TResult result) {
_map.Add(_currentCase, result);
return this;
}
public TResult Default(TResult defaultResult) {
TResult result;
if (_map.TryGetValue(_element, out result)) {
return result;
}
return defaultResult;
}
}
要创建这样的代码:
return new Switch<int, string>(a)
.Case(1).Then("lalala")
.Case(2).Then("blabla")
.Case(3).Then("lololo")
.Default("default");
可惜编译器无法推断出类型参数,感觉有点笨拙。将Default
触发“开关”的评估,并且必须是链中的最后一个方法调用。请注意,您始终需要一个默认值,因为您已经变成switch
了一个表达式。
更新:您可以使用以下代码解决类型推断问题并促使用户做正确的事情:
public static class Switch {
public static SwitchBuilder<TElement>.CaseBuilder On<TElement>(TElement element) {
return new SwitchBuilder<TElement>(element).Start();
}
public class SwitchBuilder<TElement> {
TElement _element;
TElement _firstCase;
internal SwitchBuilder(TElement element) { _element = element; }
internal CaseBuilder Start() {
return new CaseBuilder() { Switch = this };
}
private ThenBuilder Case(TElement element) {
_firstCase = element;
return new ThenBuilder() { Switch = this };
}
private SwitchBuilder<TElement, TResult>.CaseBuilder Then<TResult>(TResult result) {
return new SwitchBuilder<TElement, TResult>(
_element,
_firstCase,
result).Start();
}
public class CaseBuilder {
internal SwitchBuilder<TElement> Switch { get; set; }
public ThenBuilder Case(TElement element) {
return Switch.Case(element);
}
}
public class ThenBuilder {
internal SwitchBuilder<TElement> Switch { get; set; }
public SwitchBuilder<TElement, TResult>.CaseBuilder Then<TResult>(TResult result) {
return Switch.Then(result);
}
}
}
public class SwitchBuilder<TElement, TResult> {
TElement _element;
TElement _currentCase;
IDictionary<TElement, TResult> _map = new Dictionary<TElement, TResult>();
internal SwitchBuilder(TElement element, TElement firstCase, TResult firstResult) {
_element = element;
_map.Add(firstCase, firstResult);
}
internal CaseBuilder Start() {
return new CaseBuilder() { Switch = this };
}
private ThenBuilder Case(TElement element) {
_currentCase = element;
return new ThenBuilder() { Switch = this };
}
private CaseBuilder Then(TResult result) {
_map.Add(_currentCase, result);
return new CaseBuilder() { Switch = this };
}
private TResult Default(TResult defaultResult) {
TResult result;
if (_map.TryGetValue(_element, out result)) {
return result;
}
return defaultResult;
}
public class CaseBuilder {
internal SwitchBuilder<TElement, TResult> Switch { get; set; }
public ThenBuilder Case(TElement element) {
return Switch.Case(element);
}
public TResult Default(TResult defaultResult) {
return Switch.Default(defaultResult);
}
}
public class ThenBuilder {
internal SwitchBuilder<TElement, TResult> Switch { get; set; }
public CaseBuilder Then(TResult result) {
return Switch.Then(result);
}
}
}
}
结果就是这个漂亮的、类型安全的、流畅的界面;在每个步骤中,您只能选择正确的调用方法(例如Then
after Case
):
return Switch.On(a)
.Case(1).Then("lalala")
.Case(2).Then("blabla")
.Case(3).Then("lololo")
.Default("default");