可以使用 Protobuf-net 部分序列化一个对象吗?

发布于 2024-11-13 05:58:12 字数 401 浏览 1 评论 0原文

我希望能够通过仅序列化/反序列化更改的字段来更新对象。 我正在使用序列化器的非通用版本,因为我不知道编译时的类型。在运行时,我确实有这种类型。

在本地,我想做类似的事情:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为你拒绝所有暧昧 2024-11-20 05:58:12

你有几个选择。

如果您的类型内部知道发生了什么变化,您可以使用与 XmlSerializer (IIRC) 相同的模式,即

[ProtoMember(12)]
public string Foo {get;set;}

public bool ShouldSerializeFoo() {
    return ... true if Foo is dirty
}

第二个选项是动态创建模型,并且只告诉它有关已更改的成员的信息。但是,由于默认情况下这会导致(随着时间的推移)生成大量动态代码,因此在这种情况下您可能需要将 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.

[ProtoMember(12)]
public string Foo {get;set;}

public bool ShouldSerializeFoo() {
    return ... true if Foo is dirty
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文