强制protobuf-net忽略IEnumerable/ICollection接口

Ama*_*duh 6 c# wpf serialization .net-4.0 protobuf-net

如何让protobuf-net的v2忽略我的类实现ICollection,IEnumerable等的事实?

对于这种特殊情况,我只希望标记为[ProtoMember]的字段被序列化.


我目前正在使用protobuf-net v1转换为使用v2.我有一个特殊的结构,由于更改,现在正在序列化错误.它看起来像下面这样:

[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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

编写FileTree类是为了将文件路径(如"C:\ happy.txt""C:\ history.txt")折叠成更像的东西

"C:\h"
???? "appy.txt"
???? "istory.txt"
Run Code Online (Sandbox Code Playgroud)

该结构消除了路径串中的冗余.所以,我真的不希望FileTree类通过IEnumerable函数被序列化,因为它只是存储为"C:\ happy.txt","C:\ history.txt"等.现在,在序列化中对于FileTree对象,每个路径都将完全打印出来.


编辑:我要提到的最后一件事 - 我在FileTree中有一个On_Deserialization函数,它用[ProtoAfterDeserialization]标记.我在函数中放了一个断点,但它没有被击中.这与此类序列化的方式有关吗?

Mar*_*ell 8

[ProtoContract(IgnoreListHandling = true)]
public class FileTree : ICollection<FilePath> ...
{ ... }
Run Code Online (Sandbox Code Playgroud)

应该这样做.老实说,我不认为我已经考虑过列表上的回调,因为它们的处理方式与实体非常不同,但上面的内容应该可行.如果没有,请告诉我.

从intellisense文档:

获取或设置一个值,指示不应将此类型视为列表,即使它具有熟悉的类似列表的特征(可枚举,添加等)