using System; using System.Collections.Generic; namespace AibisDream.FixSystem { /// /// 以两个Key为索引的Dic /// /// Value类型 public class StateDictionary { private readonly Dictionary _stateDic = new(); public T Get(FixState source, FixState target) { var key = new StateTransKey(source, target); return _stateDic[key]; } public void Add(FixState source, FixState target, T value) { var key = new StateTransKey(source, target); _stateDic[key] = value; } public void Clear() { _stateDic.Clear(); } public bool ContainsKey(FixState source, FixState target) { var key = new StateTransKey(source, target); return _stateDic.ContainsKey(key); } public bool TryGetValue(FixState source, FixState target, out T value) { var key = new StateTransKey(source, target); return _stateDic.TryGetValue(key, out value); } /// /// 用于实现两个Key的Dictionary /// private class StateTransKey { private readonly FixState _sourceState; private readonly FixState _targetState; public StateTransKey(FixState sourceState, FixState targetState) { _sourceState = sourceState; _targetState = targetState; } public override bool Equals(object obj) { // 类型不同肯定不相等 if (obj is not StateTransKey other) return false; // 判断两个key相等 return _sourceState == other._sourceState && _targetState == other._targetState; } public override int GetHashCode() { return HashCode.Combine(_sourceState, _targetState); } } } }