简体   繁体   中英

Serializing a List<IComparable> member with protobuf-net

I'm trying to serialize an existing legacy class using 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;
        }
    }
}

I get an exception saying No serializer for type System.IComparable is available for model (default) .
Looking at some examples, I've seen that I should add the DynamicType = true property in the ProtoMember attribute, but it's now deprecated.
Is there an alternative way to serialize and deserialize a class with a member declared like that?

No, fundamentally. Protobuf is predicated on knowing what you're serializing / deserializing before you do it, which isn't possible if all you know is object or IComparable .

I spoke about this situation in more detail last week in a GitHub issue here , including recommendations on how I would represent this if it was me, which effectively comes down to "as a discriminated union of the actual types I expect". I would also create a separate DTO model, and serialize that rather than trying to serialize my pre-existing type - and then just map between them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM