C# protobuf net序列化为字符串并存储在数据库中,然后反序列化
我想使用字符串序列化/反序列化对象。需要注意的是,当我序列化/反序列化到一个文件时,一切正常。我试图做的是获取一个字符串,这样我就可以将其存储在数据库中,然后稍后将其取出进行反序列化 以下是有效的代码:C# protobuf net序列化为字符串并存储在数据库中,然后反序列化,c#,protobuf-net,C#,Protobuf Net,我想使用字符串序列化/反序列化对象。需要注意的是,当我序列化/反序列化到一个文件时,一切正常。我试图做的是获取一个字符串,这样我就可以将其存储在数据库中,然后稍后将其取出进行反序列化 以下是有效的代码: MemoryStream msTest = new MemoryStream(); Serializer.Serialize(msTest, registrationBlocks); msTest.Position = 0; List<RVRegistrationBlock> Cop
MemoryStream msTest = new MemoryStream();
Serializer.Serialize(msTest, registrationBlocks);
msTest.Position = 0;
List<RVRegistrationBlock> CopiedBlocks = new List<RVRegistrationBlock>();
CopiedBlocks = Serializer.Deserialize<List<RVRegistrationBlock>>(msTest);
MemoryStream msTest=newmemoryStream();
序列化(msTest,registrationBlocks);
msTest.Position=0;
List CopiedBlocks=新列表();
CopiedBlocks=序列化程序。反序列化(msTest);
“CopiedBlocks”对象与“registrationBlocks”中的列表相同,非常好,所有内容都已序列化/反序列化。我把所有的东西都保存在这里
下面是当我尝试将字符串包含在内时无法工作的代码:
MemoryStream msTestString = new MemoryStream();
Serializer.Serialize(msTestString, registrationBlocks);
msTestString.Position = 0;
StreamReader srRegBlock = new StreamReader(msTestString);
byte[] bytedata64 = System.Text.Encoding.Default.GetBytes(srRegBlock.ReadToEnd());
string stringBase64 = Convert.ToBase64String(bytedata64);
byte[] byteAfter64 = Convert.FromBase64String(stringBase64);
MemoryStream afterStream = new MemoryStream(byteAfter64);
List<RVRegistrationBlock> CopiedBlocksString = new List<RVRegistrationBlock>();
CopiedBlocksString = Serializer.Deserialize<List<RVRegistrationBlock>>(afterStream);
MemoryStream msTestString=new MemoryStream();
Serializer.Serialize(msTestString,registrationBlocks);
msTestString.Position=0;
StreamReader srRegBlock=新的StreamReader(msTestString);
byte[]bytedata64=System.Text.Encoding.Default.GetBytes(srRegBlock.ReadToEnd());
string stringBase64=Convert.tobase64字符串(bytedata64);
byte[]byteAfter64=Convert.FromBase64String(stringBase64);
MemoryStream afterStream=新的MemoryStream(byteAfter64);
List CopiedBlocksString=新列表();
CopiedBlocksString=序列化程序。反序列化(afterStream);
在最后一行进行反序列化时,我得到一个异常:抛出了类型为“ProtoBuf.ProtoException”的异常。我无法深入研究它,内部异常为null。我不明白它为什么这么做
我肯定把范围缩小到了这样一个事实,当我牵扯到一根绳子的时候,它就会失控。我将字符串存储在nvarchar(max)中的数据库中,这就是我想要字符串的原因
任何帮助都将不胜感激 我对使用
StreamReader
有点不知所措。在这种情况下,我认为您可以忽略这一点,并执行如下操作,以确保不会发生单向编码
MemoryStream msTestString = new MemoryStream();
Serializer.Serialize(msTestString, registrationBlocks);
string stringBase64 = Convert.ToBase64String(msTestString.ToArray());
byte[] byteAfter64 = Convert.FromBase64String(stringBase64);
MemoryStream afterStream = new MemoryStream(byteAfter64);
List<RVRegistrationBlock> CopiedBlocksString = new List<RVRegistrationBlock>();
CopiedBlocksString = Serializer.Deserialize<List<RVRegistrationBlock>>(afterStream);
MemoryStream msTestString=new MemoryStream();
Serializer.Serialize(msTestString,registrationBlocks);
string stringBase64=Convert.ToBase64String(msTestString.ToArray());
byte[]byteAfter64=Convert.FromBase64String(stringBase64);
MemoryStream afterStream=新的MemoryStream(byteAfter64);
List CopiedBlocksString=新列表();
CopiedBlocksString=序列化程序。反序列化(afterStream);
根据答案和评论,我使用以下内容:
internal static string SerializeToString_PB<T>(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<T>(this string txt)
{
byte[] arr = Convert.FromBase64String(txt);
using (MemoryStream ms = new MemoryStream(arr))
return ProtoBuf.Serializer.Deserialize<T>(ms);
}
内部静态字符串序列化为字符串\u PB(此T obj)
{
使用(MemoryStream ms=new MemoryStream())
{
ProtoBuf.Serializer.Serialize(ms,obj);
返回Convert.ToBase64String(ms.GetBuffer(),0,(int)ms.Length);
}
}
内部静态T反序列化fromstring_PB(此字符串为txt)
{
字节[]arr=Convert.FromBase64String(txt);
使用(MemoryStream ms=新的MemoryStream(arr))
返回ProtoBuf.Serializer.Deserialize(ms);
}
公共静态字符串ToProtobufString(此对象模型){
使用var memString=newmemoryStream();
Serializer.Serialize(memString,model);
返回Convert.ToBase64String(memString.GetBuffer(),0,(int)memString.Length);
}
公共静态T FromProtobufString(此字符串为protobuf),其中T:class{
var byteAfter64=Convert.FromBase64String(protobuf);
使用var mem=新内存流(byteAfter64);
返回mem.FromProtobuf();
}
回答得好;如果需要,您可以进行一个较小的优化-Convert.ToBase64String(msTestString.GetBuffer(),0,(int)msTestString.Length)
,这样可以避免创建额外的字节[]
。但它会“按原样”工作。