public static object BytesToObj(int hash, byte[] data, int offset, int length) { TypeData td = TypeMapping.FindType(hash); if (td == null) { LogMgr.Log("未注册的类型:" + hash.ToString()); return(null); } System.IO.MemoryStream ms = new MemoryStream(data, offset, length); BinaryReader bin = new BinaryReader(ms); return(CreateObj(td, bin)); }
public static byte[] ObjToBytes(object obj, int hash, int prefixLength) { TypeData td = TypeMapping.FindType(hash); if (td == null) { LogMgr.Log("未注册的类型:" + hash.ToString()); return(null); } MemoryStream ms = new MemoryStream(); BinaryWriter bin = new BinaryWriter(ms); if (!ConvertToBytes(td, obj, bin)) { return(null); } byte[] bytes = new byte[ms.Length + prefixLength]; byte[] data = ms.GetBuffer(); Array.Copy(data, 0, bytes, prefixLength, ms.Length); return(bytes); }