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

本文关键字:ICollection 接口 IEnumerable protobuf-net 忽略 强制 | 更新日期: 2023-09-27 18:16:26

我怎么能得到v2的protobuf-net忽略的事实,我的类实现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; }
    }
}

FileTree类被编写为将像"C:'happy.txt" "C:'history.txt"这样的文件路径折叠成更像

的内容。
"C:'h"
└─── "appy.txt"
└─── "istory.txt"

该结构消除了路径字符串中的冗余。所以,我真的不希望FileTree类通过IEnumerable函数被序列化,因为这样它就会被存储为"C:'happy.txt","C:'history.txt"等。现在,在一个FileTree对象的序列化过程中,每个路径都被完整地打印出来。


EDIT:我应该提到的最后一件事—我在FileTree中有一个On_Deserialization函数,标记为[ProtoAfterDeserialization]。我在函数中设置了一个断点,但它没有被击中。这与这个类被序列化的方式有关吗?

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

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

应该做这件事。老实说,我认为我没有考虑过列表上的回调,因为它们的处理方式与实体非常不同,但有了上面的方法,应该可以工作。如果没有,请告诉我。

来自智能感知文档:

获取或设置一个值,该值指示该类型不应被视为列表,即使它具有熟悉的类似列表的特征(enumerable, add等)