如何POCO(德)序列化工作在protobuf网?序列化、工作、POCO、protobuf

2023-09-06 21:55:28 作者:木槿昔年。

是有可能的(de)既没有使用protobuf网属性也没有明确添加类型到模型序列化POCO类型?

Is it possible to (de)serialize a POCO type using neither protobuf-net attributes nor explicitly adding types into the model?

推荐答案

目前, - 总之,没有。它需要有一个的基本的你打算如何对它的理解来操作。我想也许我可以添加一些东西,让你的指定的为完全缦类型(事情是不是默认的策略 DataContract ProtoContract XmlType将),但最合适的选择会有所有公共成员(很像的XmlSerializer )。

At the moment - in short, no. It needs to have a basic understanding of how you intend it to operate. I guess maybe I could add something to let you specify a default strategy for completely unadorned types (things that aren't DataContract, ProtoContract or XmlType), but the most appropriate option there would be "all public members" (much like XmlSerializer).

我不希望鼓励其原因是,它是脆性的。因为如何protobuf的规范定义,你得到的是现场的数字。这是很容易说OK,责令其按字母顺序排列,并用自己的立场,但不是安全的,如果你想改变的类型。而让我们面对现实吧,我们都这样做。你会惊奇地发现我常常添加 AardvarkCount 属性,它与食堂按字母排序。

The reason I don't want to encourage this is that it is brittle. Because of how the protobuf spec is defined, all you get is field-numbers. It is easy enough to say "ok, order them alphabetically and use their positions", but that is not safe if you ever want to change the type. And let's face it, we all do. You'd be amazed how often I add an AardvarkCount property, which messes with alphabetical orderings.

因此​​,我不想让它很容易让人们进入一个位置,他们的风险数据的完整性。因为我不喜欢别人喊我。如果有一个全球性的默认策略,这将是易于使用的政策的没有意识到这一点的,也就是当你开始陷入麻烦。

Hence, I don't want to make it easy to get people into a position where they risk data integrity. Because I don't like people shouting at me. If there was a global default policy, it would be easy to use that policy without realising it, which is when you start getting into trouble.

我这样做,不过,打算使它更容易选择在每个类型的基础上的策略(在code都存在,它只是没有在公共API) - 例如:

I do, however, intend making it easier to choose those strategies on a per-type basis (the code all exists, it just isn't in the public API) - for example:

model.Add(typeof(MyCrazyType), false).ApplyPolicy(ImplicitFields.AllPublic);

(即 ImplicitFields 的东西已经存在,回V1天),或者只是:

(the ImplicitFields stuff already exists, back to v1 days) or maybe just:

model.Add(typeof(MyCrazyType), ImplicitFields.AllPublic);

任何帮助吗?没有推理是否有意义?

any help? does the reasoning make sense?