ProtoBuf-net serializing IEnumerable

前端 未结 2 1072
轻奢々
轻奢々 2020-12-19 03:40

I\'m trying to use ProtoBuf-NET in my project (it\'s mostly Silverlight 4 project).

I\'m having difficulties serializing my Model collections, they all are defined l

2条回答
  •  清酒与你
    2020-12-19 04:32

    1. not at current, no; the type exposed must have (at a minimum) an Add method. While I have no objection to investigating the possibility of an Add outside the object itself, this is complicated since you are then looking at a different "primary" object (the sequence vs the container). However; if your parent object implemented IEnumerable (returning _itemsSet.GetEnumerator() etc), then it would find the Add automatically

    2. I can't see the context here; however, I suspect that without the Add it still isn't happy to consider it a list in the first place. I see the way you are going with that, though, and it is perhaps a way it could reason "I can use a List here)

    3. It isn't something I've investigated, to be honest; so: no

    4. The type exposed in the property must, as a minimum: implement IEnumerable (although IEnumerable would be preferred), and expose an Add(T) method. It doesn't have to be Collection / List / etc - simply: it must (at present) have some mechanism to add. IList would be a a pragmatic option, but I gather that isn't quite what you want.

    Jonathan is right that a surrogate for the outer-class (the thing which has _itemsSet, TSet and AddT) might also be an option.

    If the outer-class only exists to have the collection and the add method, then just adding : IEnumerable and renaming AddT to Add would probably make it work.

提交回复
热议问题