switch (_mediaState) { case State.Run: AAA(); break; case State.Stop: BBB(); break; case State.Forward: CCC(); break; case State.Rewind: DDD(); break; }
public delegate void RelaceSwitch(); private static Dictionary<State,RelaceSwitch> ReplaceSwitch=new Dictionary<State, RelaceSwitch>(); public static void MethodRun() { AAA(); } public static void MethodStop() { BBB(); } public static void MethodForward() { CCC(); } public static void MethodRewind() { DDD(); } private static void Adddic() { ReplaceSwitch.Add(State.Run, MethodRun);//ReplaceSwitch[State.Run]=MethodRun; ReplaceSwitch.Add(State.Stop, MethodStop); ReplaceSwitch.Add(State.Forward, MethodForward); ReplaceSwitch.Add(State.Rewind, MethodRewind); } private static void Exec(State state, RelaceSwitch action) { action.Invoke(); }
调用
去掉switch
首先Adddic();
之后Exec(_mediaState, ReplaceSwitch[_mediaState]);