可以使用 Protobuf-net 部分序列化一个对象吗?
我希望能够通过仅序列化/反序列化更改的字段来更新对象。 我正在使用序列化器的非通用版本,因为我不知道编译时的类型。在运行时,我确实有这种类型。
在本地,我想做类似的事情:
var existingObject.SomeField = 10;
// Say I only want to serialize field B
byte[] serializedField = SerializeField(existingObject, "SomeField")
远程我会反序列化并创建一个新对象:
Merge(serializedField, existingObject);
似乎没有办法使用 NonGeneric 接口来执行此操作?
I would like to be able to update objects by serializing/deserializing only the field that changed.
I am using the nongeneric version of the serializer since I don't know the type at compile. At runtime, I do have the type though.
Locally I want to do something like:
var existingObject.SomeField = 10;
// Say I only want to serialize field B
byte[] serializedField = SerializeField(existingObject, "SomeField")
Remotely I would deserialize and create a new object:
Merge(serializedField, existingObject);
There does not seem to be a way to do this using the NonGeneric interface?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有几个选择。
如果您的类型内部知道发生了什么变化,您可以使用与 XmlSerializer (IIRC) 相同的模式,即
第二个选项是动态创建模型,并且只告诉它有关已更改的成员的信息。但是,由于默认情况下这会导致(随着时间的推移)生成大量动态代码,因此在这种情况下您可能需要将 AutoCompile 设置为 fse。
第三种选择是通过 ProtoWriter 手动序列化。这可能需要比预期更多的 protobuf 专业知识。
You have a couple of options there.
If your type internally knows what has changed, you can use the same pattern as XmlSerializer (IIRC), I.e.
Second option would be to create the model on the fly, and only tell it about the members that are changed. However, since by default this would cause (over time) lots of dynamic code to be generated, so you might want to set AutoCompile to fse for that case.
A third option would be to serialize manually via ProtoWriter. This probably needs more protobuf know-how than is desirable.