/// <summary> /// 序列化对象,并将序列化数据写入指定的缓冲区中(线程安全) /// </summary> /// <param name="obj">被序列化的对象</param> /// <param name="buffer">用于接受序列化数据的缓冲区</param> /// <param name="index"><paramref name="buffer"/>开始写入的索引</param> /// <typeparam name="T">序列化的对象类型</typeparam> /// <returns>序列化数据的大小</returns> /// <exception cref="ArgumentException">字节数组容量不足</exception> public static int Serialize <T>(T obj, byte[] buffer, int index) { StreamingWriter writer = new StreamingWriter(GetAllocator <T>()); writer.BeginWrite(); try { SerializerInjector.InternalCache <T> .Serializer.Serialize(obj, ref writer); return(writer.CopyToArray(buffer, index)); } finally { writer.EndWrite(); } }
/// <summary> /// 序列化对象,并返回序列化数据(线程安全) /// </summary> /// <param name="obj">被序列化的对象</param> /// <typeparam name="T">序列化的对象类型</typeparam> /// <returns>对象的序列化结果</returns> public static byte[] Serialize <T>(T obj) { StreamingWriter writer = new StreamingWriter(GetAllocator <T>()); writer.BeginWrite(); try { SerializerInjector.InternalCache <T> .Serializer.Serialize(obj, ref writer); return(writer.ToArray()); } finally { writer.EndWrite(); } }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value"></param> public void WriteValue <T>(T value) { StreamingWriter writer = new StreamingWriter(SerializerInjector.InternalCache <T> .Allocator); writer.BeginWrite(); try { SerializerInjector.InternalCache <T> .Serializer.Serialize(value, ref writer); WriteValue(writer.m_ByteCount, NumberFormat.Variant); WriteBytes((byte *)writer.m_Allocator.GetMemoryPtr(0, 0).ToPointer(), writer.m_ByteCount); } finally { writer.EndWrite(); } }