using System.Collections.Generic; namespace TBL.CSharp.Serialization.Marshal.Context { /// /// Marshal 风格的序列化上下文基础实现 /// public class MarshalContextBasic : IMarshalContext { /// /// 已链接的符号 /// private readonly IDictionary _linkedSymbols = new Dictionary(); /// /// 已链接的对象 /// private readonly IDictionary _linkedObjects = new Dictionary(); /// /// 已链接的符号 Id /// private readonly IDictionary _linkedSymbolIds = new Dictionary(); /// /// 已链接的对象 Id /// private readonly IDictionary _linkedObjectIds = new Dictionary(); public void Clear() { _linkedSymbols.Clear(); _linkedSymbolIds.Clear(); _linkedObjects.Clear(); } public string GetLinkableSymbol(int index) => _linkedSymbols[index]; public string AddLinkableSymbol(string symbol) { var id = _linkedSymbols.Count + 1; _linkedSymbols.Add(id, symbol); _linkedSymbolIds.Add(symbol, id); return symbol; } public bool FindLinkableSymbol(string symbol, out int index) => _linkedSymbolIds.TryGetValue(symbol, out index); public object GetLinkableObject(int index) => _linkedObjects[index]; public T AddLinkableObject(T element) { var id = _linkedObjects.Count + 1; _linkedObjects.Add(id, element); _linkedObjectIds.Add(element, id); return element; } public bool FindLinkableObject(object element, out int index) => _linkedObjectIds.TryGetValue(element, out index); } }