使用protobuf-net序列化List <IComparable>成员

问题描述

我正在尝试使用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)
通过一些示例,我已经看到我应该在DynamicType = true属性添加ProtoMember属性,但现在已弃用该属性
有没有其他方法可以用这样声明的成员序列化和反序列化类?

解决方法

不,从根本上讲。 Protobuf的前提是要知道在进行操作之前 要进行序列化/反序列化,如果只知道objectIComparable,则不可能。

我上周在a GitHub issue here中更详细地谈到了这种情况,包括有关如果我是我该如何表示的建议,实际上归结为“作为我期望的实际类型的有区别的联合体” 。我还将创建一个单独的DTO模型,并序列化该 ,而不是尝试序列化我先前存在的类型-然后在它们之间进行映射。