protobuf-net 从 base class 中跳过一个成员(接口类型)
protobuf-net skips a member (of Interface type) from base class
我有一个 Base
class,它定义了一个名为 Member
的 IMember
类型的成员。然后我有一个 Child
class 继承了 Base
并定义了一个新成员 IMember
叫做 Member2
。
我序列化了 Child
class 的一个实例,保存在类型为 Base
的变量中,期望所有子成员和基本成员都被序列化。除了 Child
中的 Member2
被序列化,Base
class 中定义的具有相同类型的成员未被序列化,(或者至少在反序列化时我得到一个其值为 null)
我的class完全是这样的:
[ProtoContract]
[ProtoInclude(100, typeof(Member))]
public interface IMember {
[ProtoMember(1, IsRequired = true)]
int value { get; set; }
}
[ProtoContract()]
public class Member : IMember {
[ProtoMember(1, IsRequired = true)]
public int value { get; set; }
}
[ProtoContract()]
public class Base {
[ProtoMember(1, IsRequired = true)]
public virtual string Name { get; set; } = "Base";
[ProtoMember(2, IsRequired = true)]
public IMember MyMember { get; set; } = null;
}
[ProtoContract()]
public class Child : Base {
[ProtoMember(1, IsRequired = true)]
public override string Name { get; set; } = "Child";
[ProtoMember(2, IsRequired = true)]
public IMember MyMember2 { get; set; } = null;
}
测试代码是这样的:
static void Main(string[] args) {
Base b = new Child() {
MyMember = new Member() { value = 10 },
MyMember2 = new Member() { value = 20 }
};
var stream = new MemoryStream();
Serializer.Serialize(stream, b);
var bytes = stream.ToArray();
stream = new MemoryStream(bytes);
var obj = Serializer.Deserialize<Child>(stream);
Console.ReadLine();
}
反序列化对象 obj
有 Member == null
而 Member2 != null
。这是为什么?
基础 class 缺少用于指定层次结构的 [ProtoInclude] 属性。
[ProtoContract()]
[ProtoInclude(100, typeof(Child))]
public class Base {
[ProtoMember(1, IsRequired = true)]
public virtual string Name { get; set; } = "Base";
[ProtoMember(2, IsRequired = true)]
public IMember MyMember { get; set; } = null;
}
我有一个 Base
class,它定义了一个名为 Member
的 IMember
类型的成员。然后我有一个 Child
class 继承了 Base
并定义了一个新成员 IMember
叫做 Member2
。
我序列化了 Child
class 的一个实例,保存在类型为 Base
的变量中,期望所有子成员和基本成员都被序列化。除了 Child
中的 Member2
被序列化,Base
class 中定义的具有相同类型的成员未被序列化,(或者至少在反序列化时我得到一个其值为 null)
我的class完全是这样的:
[ProtoContract]
[ProtoInclude(100, typeof(Member))]
public interface IMember {
[ProtoMember(1, IsRequired = true)]
int value { get; set; }
}
[ProtoContract()]
public class Member : IMember {
[ProtoMember(1, IsRequired = true)]
public int value { get; set; }
}
[ProtoContract()]
public class Base {
[ProtoMember(1, IsRequired = true)]
public virtual string Name { get; set; } = "Base";
[ProtoMember(2, IsRequired = true)]
public IMember MyMember { get; set; } = null;
}
[ProtoContract()]
public class Child : Base {
[ProtoMember(1, IsRequired = true)]
public override string Name { get; set; } = "Child";
[ProtoMember(2, IsRequired = true)]
public IMember MyMember2 { get; set; } = null;
}
测试代码是这样的:
static void Main(string[] args) {
Base b = new Child() {
MyMember = new Member() { value = 10 },
MyMember2 = new Member() { value = 20 }
};
var stream = new MemoryStream();
Serializer.Serialize(stream, b);
var bytes = stream.ToArray();
stream = new MemoryStream(bytes);
var obj = Serializer.Deserialize<Child>(stream);
Console.ReadLine();
}
反序列化对象 obj
有 Member == null
而 Member2 != null
。这是为什么?
基础 class 缺少用于指定层次结构的 [ProtoInclude] 属性。
[ProtoContract()]
[ProtoInclude(100, typeof(Child))]
public class Base {
[ProtoMember(1, IsRequired = true)]
public virtual string Name { get; set; } = "Base";
[ProtoMember(2, IsRequired = true)]
public IMember MyMember { get; set; } = null;
}