我想通过 protobuf.net 序列化一个抽象示例的属性。
你能解释一下我需要改变什么以便这个简单的例子有效吗?
using System;
using System.IO;
using ProtoBuf;
namespace TestProtobuf
{
class Program
{
static void Main(string[] args)
{
var x = new ClassB {A = "AAAA", B = "BBBB"};
var memoryStream = new MemoryStream();
Serializer.Serialize(memoryStream,x);
var memoryStream2 = new MemoryStream(memoryStream.ToArray());
var y = Serializer.Deserialize<ClassB>(memoryStream2);
if (x.A != y.A)
Console.Write("Failed");
}
}
[ProtoContract]
public class ClassB:ClassA
{
[ProtoMember(1)]
public string B { get; set; }
}
[ProtoContract]
public abstract class ClassA
{
[ProtoMember(2)]
public string A { get; set; }
}
}