12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- using System.Collections.Generic;
- namespace TBL.CSharp.Serialization.Marshal.Context
- {
- /// <summary>
- /// Marshal 风格的序列化上下文基础实现
- /// </summary>
- public class MarshalContextBasic : IMarshalContext
- {
- /// <summary>
- /// 已链接的符号
- /// </summary>
- private readonly IDictionary<int, string> _linkedSymbols =
- new Dictionary<int, string>();
- /// <summary>
- /// 已链接的对象
- /// </summary>
- private readonly IDictionary<int, object> _linkedObjects =
- new Dictionary<int, object>();
- /// <summary>
- /// 已链接的符号 Id
- /// </summary>
- private readonly IDictionary<string, int> _linkedSymbolIds =
- new Dictionary<string, int>();
- /// <summary>
- /// 已链接的对象 Id
- /// </summary>
- private readonly IDictionary<object, int> _linkedObjectIds =
- new Dictionary<object, int>();
- 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>(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);
- }
- }
|