我有兩個簡單的類,我想用protobuf-net序列化。一切看起來不錯,但是當我反序列化Body屬性爲null時。任何想法我做錯了什麼?Newbe試圖用protobuf-net序列化對象
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; }
}
}
任何想法如何使當前的示例與對象一起工作? – mynkow
@mynkow是;編輯於 –
10x Marc。當body是object類型時,RuntimeTypeModel有效。困擾我的是Inner可以是任何類型。例如Outer是EventMessage的包裝,每個EventMessage都有不同的字段或屬性。你是否建議使用反射來將這些事件消息添加到condif中? – mynkow