如何在 MessagePack 中使用 Primitive API?

How to use Primitive API in MessagePack?

我尝试像这样序列化和反序列化:

 var mem = new MemoryStream();
 MessagePackBinary.WriteInt64(mem, 1580358);
 var result = MessagePackBinary.ReadInt64(mem);` 
 //System.InvalidOperationException: 'Invalid MessagePack code was detected, code:-1'

但是我有错误。 我做错了什么? 谢谢!

使用库:MessagePack repo

您应该将流倒回到初始位置以读回您在那里写的内容:

var mem = new MemoryStream();
MessagePackBinary.WriteInt64(mem, 1580358);
mem.Seek(0, SeekOrigin.Begin); // added
var result = MessagePackBinary.ReadInt64(mem);
Console.WriteLine(result);