Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force protobuf-net to ignore IEnumerable/ICollection interfaces

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?

like image 654
Amanduh Avatar asked Dec 02 '11 20:12

Amanduh


1 Answers

[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)

like image 146
Marc Gravell Avatar answered Sep 24 '22 18:09

Marc Gravell