c# - 强制 protobuf-net 忽略 IEnumerable/ICollection 接口(interface)

标签 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; }
    }
}

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/

相关文章:

面向初学者的 C# GUI 编程 : Where to start?

wpf - 如何在 Windows 中制作安装程序 (exe) 文件

c# - 使用 C# 使用 web 服务 php

c# - Visual Studio C# 2010 快速调试运行速度比发布快

c# - Entity Framework 中可重用的 linq 选择查询

c# - 对已实例化的对象使用 using 语句

C# WPF datagrid 鼠标悬停显示图像

c# - MahApps Metro 和 DataGridExtensions

python - 如何在 C(++) 中(反)序列化 PyObject*?

hadoop - 在Spark中读取级联序列文件