【问题标题】:Protobuf-net skip deserialization of specific fieldsProtobuf-net 跳过特定字段的反序列化
【发布时间】:2015-02-11 00:27:09
【问题描述】:

我已经序列化了这个类:

[ProtoContract]
public class TestClass
{
    [ProtoMember(1)] public int[] hugeArray;
    [ProtoMember(2)] public int x;
    [ProtoMember(3)] public int y;
    //lot more fields and properties to serialize here...

}

如何在反序列化过程中跳过 [ProtoMember(1)] hugeArray,从而只反序列化 x、y 和其他字段?

我的问题是,有时我很快只需要获取“元数据”,这是其他字段和属性所描述的,但有时我需要整个对象。

【问题讨论】:

    标签: serialization protobuf-net


    【解决方案1】:

    两种选择:

    • 两个RuntimeTypeModel 实例(一个手动构建,只指定了所需的字段)
    • 两种类型;即创建一个更简单的TestClass简单地省略大字段 - 即TestClassMetadata - 并反序列化为那个; protobuf-net 完全不介意

    【讨论】:

    • 我的班级有 30 个字段,我试图避免重复代码只是为了在反序列化期间跳过 1 个字段。我想要类似(伪代码):RuntimeTypeModel.BuildModelFor(typeof(TestClass)).RemoveMember(1),但我不认为 protobuf-net 提供了类似的东西。
    • @Michal 确实 - 没有删除;需要 TypeModel.Create(), Add(type, false) 然后添加你确实想要的成员
    猜你喜欢
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多