使用 protobuf-net 序列化一个 List<IComparable> 成员
Serializing a List<IComparable> member with protobuf-net
我正在尝试使用 protobuf-net
:
序列化现有遗留 class
using ProtoBuf;
using System;
using System.Collections.Generic;
[ProtoContract]
public class AttributeValue {
List<IComparable> attributeValues;
[ProtoMember(1)]
public List<IComparable> Values {
get {
return this.attributeValues;
}
}
}
我得到一个异常 No serializer for type System.IComparable is available for model (default)
。
查看一些示例,我发现我应该在 ProtoMember
属性中添加 DynamicType = true
属性,但现在已弃用。
有没有另一种方法可以使用这样声明的成员序列化和反序列化 class?
不,从根本上说。 Protobuf 的前提是在 之前 知道你在序列化/反序列化什么,如果你只知道 object
或 IComparable
,这是不可能的。 =13=]
我上周在 a GitHub issue here 中更详细地谈到了这种情况,包括关于如果是我的话我将如何表示的建议,这实际上归结为“作为我实际类型的受歧视联合预计”。我还会创建一个单独的 DTO 模型,并序列化 that 而不是尝试序列化我的 pre-existing 类型 - 然后在它们之间进行映射。
我正在尝试使用 protobuf-net
:
using ProtoBuf;
using System;
using System.Collections.Generic;
[ProtoContract]
public class AttributeValue {
List<IComparable> attributeValues;
[ProtoMember(1)]
public List<IComparable> Values {
get {
return this.attributeValues;
}
}
}
我得到一个异常 No serializer for type System.IComparable is available for model (default)
。
查看一些示例,我发现我应该在 ProtoMember
属性中添加 DynamicType = true
属性,但现在已弃用。
有没有另一种方法可以使用这样声明的成员序列化和反序列化 class?
不,从根本上说。 Protobuf 的前提是在 之前 知道你在序列化/反序列化什么,如果你只知道 object
或 IComparable
,这是不可能的。 =13=]
我上周在 a GitHub issue here 中更详细地谈到了这种情况,包括关于如果是我的话我将如何表示的建议,这实际上归结为“作为我实际类型的受歧视联合预计”。我还会创建一个单独的 DTO 模型,并序列化 that 而不是尝试序列化我的 pre-existing 类型 - 然后在它们之间进行映射。