MessagePack 序列化
public static byte[] ToBin(this object obj) { return MessagePack.MessagePackSerializer.Serialize(obj.GetType(), obj); } public static T FromBin<T>(this byte[] bytes) { ReadOnlyMemory<byte> temp = new ReadOnlyMemory<byte>(bytes); return (T)MessagePack.MessagePackSerializer.Deserialize(typeof(T), temp); } public static T ToObj<T>(this object obj) { var opt = MessagePackSerializerOptions.Standard.WithResolver( MessagePack.Resolvers.ContractlessStandardResolverAllowPrivate.Instance); var bytes = MessagePack.MessagePackSerializer.Serialize(obj.GetType(), obj, opt); ReadOnlyMemory<byte> temp = new ReadOnlyMemory<byte>(bytes); return (T)MessagePack.MessagePackSerializer.Deserialize(typeof(T), temp, opt); }
posted on 2020-10-24 12:24 jonney_wang 阅读(421) 评论(0) 编辑 收藏 举报