如何让 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; }
}
}
FileTree 类被编写为将“C:\happy.txt”“C:\history.txt”等文件路径折叠成更类似的东西
"C:\h"
└─── "appy.txt"
└─── "istory.txt"
该结构消除了路径字符串中的冗余。所以,我真的不希望通过 IEnumerable 函数序列化 FileTree 类,因为这样它就被存储为“C:\happy.txt”、“C:\history.txt”等。现在,在序列化中FileTree 对象的每个路径都被完整地打印出来。
编辑: 我要提的最后一件事 -- 我在 FileTree 中有一个 On_Deserialization 函数,它被标记为 [ProtoAfterDeserialization]。我在函数中放置了一个断点,但它没有被击中。这与此类的序列化方式有关吗?
最佳答案
[ProtoContract(IgnoreListHandling = true)]
public class FileTree : ICollection<FilePath> ...
{ ... }
应该这样做。老实说,我认为我没有考虑过列表上的回调,因为它们的处理方式与实体非常不同,但上面的内容应该可以工作。如果没有,请告诉我。
来自智能感知文档:
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)
关于c# - 强制 protobuf-net 忽略 IEnumerable/ICollection 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8362571/