I'm trying to create a base class that works like a state machine and that can accept any type of enum:
public class BaseFSM <T> where T : struct, IConvertible { //Basic class that denote the transition between one state and another public class StateTransition { public T currentState { get; set; } public T nextState { get; set; } //StateTransition Constructor public StateTransition(T currentState, T nextState) { this.currentState = currentState; this.nextState = nextState; } public override int GetHashCode() { return 17 + 31 * this.currentState.GetHashCode() + 31 * this.nextState.GetHashCode();; } public override bool Equals(object obj) { StateTransition other = obj as StateTransition; return other != null && this.currentState as Enum == other.currentState as Enum && this.nextState as Enum == other.nextState as Enum; } } protected Dictionary<StateTransition, T> transitions; //All the transitions inside the FSM public T currentState; public T previusState; protected BaseFSM() { // Throw Exception on static initialization if the given type isn't an enum. if(!typeof (T).IsEnum) throw new Exception(typeof(T).FullName + " is not an enum type."); } private T GetNext(T next) { StateTransition transition = new StateTransition(currentState, next); T nextState; if (!transitions.TryGetValue(transition, out nextState)) throw new Exception("Invalid transition: " + currentState + " -> " + next); return nextState; } } As you can see I defined both GetHashCode() and Equals(object obj). This is my implementation of my child class:
public class FSMPlayer : BaseFSM<PlayerState> { public FSMPlayer() : base() { this.currentState = PlayerState.Idle; this.transitions = new Dictionary<StateTransition, PlayerState> { { new StateTransition(PlayerState.Idle, PlayerState.Run), PlayerState.Run }, //0 { new StateTransition(PlayerState.Run, PlayerState.Jump), PlayerState.Jump }, //1 }; } } As you can see in my child class I'm using my PlayerState Enum to define the state transitions. The problem it's when I try to use the getNext function because the TryGetValue always return false. The GetHashCode functions seams to work very well so I can't understand where the problem is. Thanks.