Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“反序列化”;“长”;带protobuf的字符串,用于c#dods';我不能正常工作_C#_Protobuf Net - Fatal编程技术网

“反序列化”;“长”;带protobuf的字符串,用于c#dods';我不能正常工作

“反序列化”;“长”;带protobuf的字符串,用于c#dods';我不能正常工作,c#,protobuf-net,C#,Protobuf Net,很明显,我在做一些基本的错误,但我无法找出它,也找不到文档 我正在试验Marc Gravell为.NET编写的proto buf,并尝试序列化和反序列化对象。一旦一个对象包含一个“太长”的字符串(没有尝试确定大小阈值,但它只有几百个字节),我就无法正确地反序列化该字符串 这是我的代码: using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Li

很明显,我在做一些基本的错误,但我无法找出它,也找不到文档

我正在试验Marc Gravell为.NET编写的proto buf,并尝试序列化和反序列化对象。一旦一个对象包含一个“太长”的字符串(没有尝试确定大小阈值,但它只有几百个字节),我就无法正确地反序列化该字符串

这是我的代码:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using ProtoBuf;

namespace ConsoleApplication1
{
public class Program
{
    [ProtoContract]
    public class test
    {
        [ProtoMember(1)]            
        public int i;
        [ProtoMember(2)]
        public string s1;
        [ProtoMember(3)]
        public string s2;
        [ProtoMember(4)]
        public char[] arrchars;
        [ProtoMember(5)]
        public Dictionary<int, string> Dict = new Dictionary<int, string>();

    }


    static void Main(string[] args)
    {
        test var1 = new test();
        var1.i = 10;
        var1.s1 = "Hello";
        var1.arrchars = new char[] {'A', 'B', 'C'};
        var1.Dict.Add(10, "ten");
        var1.Dict.Add(5, "five");
        var1.s2 = new String('X', 520);

        string s = PBSerializer.Serialize(typeof (test), var1);

        test var2 = null;
        PBSerializer.Deserialize(s, out var2);
    }


    public static class PBSerializer
    {
        public static string Serialize(Type objType, object obj)
        {
            MemoryStream stream = new MemoryStream();
            ProtoBuf.Serializer.Serialize(stream, obj);
            // ProtoBuf.Serializer.SerializeWithLengthPrefix(stream, obj, PrefixStyle.Fixed32, 1);
            stream.Flush();
            stream.Position = 0;
            StreamReader sr = new StreamReader(stream);
            string res = sr.ReadToEnd();
            stream.Dispose();
            sr.Dispose();
            return res;
        }

        public static void Deserialize(string serializedObj, out test obj)
        {
            MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(serializedObj));
            obj = ProtoBuf.Serializer.Deserialize<test>(stream);
            // obj = ProtoBuf.Serializer.DeserializeWithLengthPrefix<test>(stream, PrefixStyle.Fixed32, 1);
            stream.Dispose();
        }

    }

}
}
使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用System.IO;
使用System.Linq;
运用系统反思;
使用系统文本;
使用ProtoBuf;
命名空间控制台应用程序1
{
公共课程
{
[原始合同]
公开课考试
{
[原成员(1)]
公共国际一级;
[原成员(2)]
公共字符串s1;
[原成员(3)]
公共字符串s2;
[原成员(4)]
公共字符[]arrchars;
[原成员(5)]
公共字典Dict=新字典();
}
静态void Main(字符串[]参数)
{
test var1=新测试();
var1.i=10;
var1.s1=“你好”;
var1.arrchars=新字符[]{'A','B','C'};
var1.Dict.Add(10,“十”);
var1.Dict.Add(5,“五”);
var1.s2=新字符串('X',520);
字符串s=PBSerializer.Serialize(typeof(test),var1);
testvar2=null;
PBSerializer.反序列化(s,out var2);
}
公共静态类序列化程序
{
公共静态字符串序列化(类型objType,对象obj)
{
MemoryStream stream=新的MemoryStream();
ProtoBuf.Serializer.Serialize(流,obj);
//ProtoBuf.Serializer.SerializeWithLengthPrefix(stream,obj,PrefixStyle.Fixed32,1);
stream.Flush();
流位置=0;
StreamReader sr=新的StreamReader(流);
string res=sr.ReadToEnd();
stream.Dispose();
高级处置();
返回res;
}
公共静态void反序列化(字符串序列化dobj,out test obj)
{
MemoryStream stream=新的MemoryStream(Encoding.ASCII.GetBytes(SerializedDobj));
obj=ProtoBuf.Serializer.Deserialize(流);
//obj=ProtoBuf.Serializer.DeserializeWithLengthPrefix(stream,PrefixStyle.Fixed32,1);
stream.Dispose();
}
}
}
}
var2.s2与var1.s2不同-它在字符串的开头有一个额外的字符,并截断字符串结尾的大部分字符。但是,如果我将var1.s2的长度更改为一个较小的数字(比如说52个字符而不是520个字符),我的问题就会消失,但我需要能够序列化长字符串。 我假设这与我设置前缀样式(?)的错误有关,或者可能我没有使用正确的编码(?)。然而,反复试验并没有帮我解决问题

我正在使用.NET3.5,并在版本444和450上进行了尝试,结果相同

谢谢。

您正在序列化二进制数据,但试图将其当作文本来读取。不是,所以不要那样做

如果必须将任意二进制数据转换为文本,请使用和

公共静态类序列化程序
{
公共静态字符串序列化(类型objType,对象obj)
{
使用(MemoryStream stream=new MemoryStream())
{
ProtoBuf.Serializer.Serialize(流,obj);
返回Convert.ToBase64String(stream.ToArray());
}
}
//理想情况下,将其更改为使用返回值而不是输出参数。。。
公共静态void反序列化(字符串序列化dobj,out test obj)
{
字节[]数据=Convert.FromBase64String(序列化dobj);
使用(内存流=新内存流(数据))
{
obj=ProtoBuf.Serializer.Deserialize(流);
}
}

我至少每隔几周就会收到这样做的人的电子邮件。我真的应该添加一个基于字符串的重载…非常感谢,Jon Skeet。现在没事了,我理解我的错误。为了强调Jon的观点:-这不是一个罕见的错误;我经常看到这一切。感谢链接,Marc,非常感谢protobuf对于.NET。
public static class PBSerializer
{
    public static string Serialize(Type objType, object obj)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            ProtoBuf.Serializer.Serialize(stream, obj);
            return Convert.ToBase64String(stream.ToArray());
        }
    }

    // Ideally change this to use a return value instead of an out parameter...
    public static void Deserialize(string serializedObj, out test obj)
    {
        byte[] data = Convert.FromBase64String(serializedObj);
        using (MemoryStream stream = new MemoryStream(data))
        {
            obj = ProtoBuf.Serializer.Deserialize<test>(stream);
        }
    }