我有两个简单的类,我想用 protobuf-net 序列化它们。一切看起来都不错,但是当我反序列化 Body 属性时为空。知道我做错了什么吗?
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using ProtoBuf;
namespace Protobuf_test
{
public class Program
{
private static void Main(string[] args)
{
var innerObject = new Inner() { Id = Guid.NewGuid() };
var outerObject = new Outer() { Body = innerObject };
using (var stream = File.Create("serialized.bin"))
{
Serializer.Serialize(stream, outerObject);
}
using (var file = File.OpenRead("serialized.bin"))
{
var deserialized = Serializer.Deserialize<Outer>(file);
}
}
}
[DataContract]
public class Inner
{
[DataMember]
public Guid Id { get; set; }
}
[DataContract]
public class Outer
{
[DataMember]
public object Body { get; set; }
}
}