开发者

How to serialize and deserialize Custom Lists with protobuf-net

I have a custom List which c开发者_开发知识库arries some attributes I need to serialize along with its containing items.

However, as soon as I add a ProtoContract attribute to my custom list, its items dont get serialized anymore.

Example:

    [ProtoContract]
    public class MyCollection : List<string> { 

    [ProtoMember(1)]
    public string MyAdditionalField;
   }

Is this not supposed to work out of the box?

The only possible workaround I can think of is using a surrogate for the custom list which contains a separate List which then contains all the data. However, we are dealing with quite a lot of custom Lists... so this would be an inefficient approach.

Thank you, TH


In common with XmlSerializer and TypeDescriptor (and possibly DataContractSerializer - I haven't checked), there is a strong divide between is a list vs contains a list. If something is a list, it doesn't have values itself.

The preferred layout there would be to either have a list or values.

At the protocol level, there is simply no way of representing that, as this is just repeated string {name} = {field} - there is simply nowhere for MyAdditionalField to go.

(update); tested, and indeed XmlSerializer behaves the same:

<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="
http://www.w3.org/2001/XMLSchema">
  <string>abc</string>
  <string>def</string>
</ArrayOfString>

DataContractSerializer hated it more, throwing:

Type 'MyCollection' is an invalid collection type since it has DataContractAttribute attribute.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜