protobuf-net Serialize To String and Store in Database Then De Serialize

后端 未结 2 459
暗喜
暗喜 2020-12-24 07:58

I\'d like to serialize/de-serialize an object using a string. Just to note, when I serialize/de-serialize to a file everything works fine. What I\'m trying to do is get a

2条回答
  •  生来不讨喜
    2020-12-24 08:28

    Based on the answer and the comment, I use these:

            internal static string SerializeToString_PB(this T obj)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    ProtoBuf.Serializer.Serialize(ms, obj);
                    return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
                }
            }
            internal static T DeserializeFromString_PB(this string txt)
            {
                byte[] arr = Convert.FromBase64String(txt);
                using (MemoryStream ms = new MemoryStream(arr))
                    return ProtoBuf.Serializer.Deserialize(ms);
            }
    

提交回复
热议问题