Paolo Tedesco
Paolo Tedesco

Reputation: 57302

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?

Upvotes: 1

Views: 387

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064204

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.

Upvotes: 1

Related Questions