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
C# Protobuf网络序列化问题_C#_Protobuf Net - Fatal编程技术网

C# Protobuf网络序列化问题

C# Protobuf网络序列化问题,c#,protobuf-net,C#,Protobuf Net,这似乎是一个相当简单的用例,我不明白下面代码段中的异常是如何抛出的 static void Main(string[] args) { using (var foobar = new MemoryStream()) { ProtoBuf.Serializer.Serialize(foobar, new Foobar()); if (foobar.Length == 0) throw new Exception("Didn't

这似乎是一个相当简单的用例,我不明白下面代码段中的异常是如何抛出的

static void Main(string[] args)
{
    using (var foobar = new MemoryStream())
    {
        ProtoBuf.Serializer.Serialize(foobar, new Foobar());
        if (foobar.Length == 0)
            throw new Exception("Didn't serialize");
    }
}

[ProtoContract]
public class Foobar
{
    [ProtoMember(1)]
    public int FoobarInt { get; set; }
}

ProtoBuf有点奇怪…零长度序列化表单本身并不是“错误”

这样想:

如果您想*反*序列化一个流并获取您试图序列化的空对象,空流就足够了(即,对象的“新”将做同样的事情),但是如果对象上有任何数据集,现在您需要实际保存/加载内容

static void Main(string[] args)
{
    using (var foobar = new MemoryStream())
    {
        var foo = new Foobar() { FoobarInt = 1 };
        ProtoBuf.Serializer.Serialize(foobar, foo);
        if (foobar.Length == 0)
            throw new Exception("Didn't serialize");
    }
}

[ProtoContract]
public class Foobar
{
    [ProtoMember(1)]
    public int FoobarInt { get; set; }
}

现在生成一个字节数组
0x08,0x01

,这是正确的。为了一个简单的例子,我过度简化了我的实际问题。我实际上使用数据契约。我有一个类,有两个属性引用其他类。我忘了在引用的类中包含数据成员属性的Order属性。这就是为什么它没有序列化任何内容。