public TarsStruct Read(TarsStruct o, int tag, bool isRequire) { //o必须有一个无参的构造函数 TarsStruct reff = null; if (SkipToTag(tag)) { try { // 必须重新创建一个,否则,会导致在同一个对象上赋值,这是由于C#的引用引起的 reff = (TarsStruct)BasicClassTypeUtil.CreateObject(o.GetType()); } catch (Exception e) { throw new TarsDecodeException(e.Message); } var hd = new HeadData(); ReadHead(hd); if (hd.Type != (byte)TarsStructType.StructBegin) { throw new TarsDecodeException("type mismatch."); } reff.ReadFrom(this); SkipToStructEnd(); } else if (isRequire) { throw new TarsDecodeException("require field not exist."); } return(reff); }
public void Write(TarsStruct o, int tag) { if (o == null) { return; } Reserve(2); WriteHead((byte)TarsStructType.StructBegin, tag); o.WriteTo(this); Reserve(2); WriteHead((byte)TarsStructType.StructEnd, 0); }