8

How can I get v2 of protobuf-net to ignore the fact that my class implements ICollection, IEnumerable, etc?

For this particular scenario, I only want the fields I have flagged as [ProtoMember] to be serialized.


I am currently in the process of converting from using protobuf-net v1 to using v2. I have a particular structure which is now serializing incorrectly because of the change. It looks something like the following:

[ProtoContract]
public class FileTree : ICollection<FilePath>, IEnumerable<FilePath>, IEnumerable, INotifyCollectionChanged, INotifyPropertyChanged {

    private FileTreeNode _Root;

    [ProtoMember (1)]
    public FileTreeNode Root {
        get { return _Root; }
        set { _Root = value; }
    }
}

The FileTree class was written to collapse file paths like "C:\happy.txt" "C:\history.txt" into something more like

"C:\h"
└─── "appy.txt"
└─── "istory.txt"

The structure eliminates redundancy in the path strings. So, I really don't want the FileTree class being serialized via the IEnumerable functions because then it just gets stored as "C:\happy.txt", "C:\history.txt", etc. Right now, in the serialization of a FileTree object, each path is getting print out in full.


EDIT: One last thing I should mention -- I have an On_Deserialization function in FileTree which is tagged with [ProtoAfterDeserialization]. I put a breakpoint in the function, but it is not getting hit. Is this related to the way this class is being serialized?

Amanduh
  • 1,233
  • 1
  • 13
  • 23
  • I'm not *aware* that this was a breaking change between v1 and v2; perhaps the subtle difference is that v1 was looking more for `IList`, or a combination of `IEnumerable` **with** a public `Add`. – Marc Gravell Dec 02 '11 at 21:44

1 Answers1

8
[ProtoContract(IgnoreListHandling = true)]
public class FileTree : ICollection<FilePath> ...
{ ... }

should do it. I honestly don't think I've considered callbacks on lists, since they are handled so very different to entities, but with the above that should work. Let me know if it doesn't.

From the intellisense documentation:

Gets or sets a value indicating that this type should NOT be treated as a list, even if it has familiar list-like characteristics (enumerable, add, etc)

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thank you! Sorry to bother you with this when the answer was so obvious in retrospect. I was looking in all the wrong places for the answer. – Amanduh Dec 02 '11 at 21:58
  • @Amanduh most things are obvious when you know the answer, however - this was a good and valid question, which fortunately happens to *have* a convenient answer (it didn't until fairly recently) – Marc Gravell Dec 02 '11 at 22:00
  • @MarcGravell Note that the IgnoreListHandling option might not work as expected in slightly more [complex](https://code.google.com/p/protobuf-net/issues/detail?id=287&q=IgnoreListHandling) [scenarios](https://code.google.com/p/protobuf-net/issues/detail?id=348&q=IgnoreListHandling). – tm1 May 13 '14 at 08:50