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);
        }
原文地址:https://www.cnblogs.com/jonney-wang/p/13868395.html