DefaultValue 属性反序列化 [英] DefaultValue properties deserialization
问题描述
我正在尝试将流反序列化为具有默认值属性的对象,而 protobuf-net 在流中缺少这些属性时只会忽略这些属性.我是否需要在反序列化之前手动将所有属性设置为其默认值?
I'm trying to deserialize stream to object with default value properties and protobuf-net just ingores this properties when they are missing in the stream. Do i need to manually set before deserialization all properties to their default values or that?
推荐答案
DefaultValues
行为(不只是在 protobuf-net - 在 System.ComponentModel
一般,例如PropertyGrid
、PropertyDescriptor
等)就是用来表示不需要序列化的东西,因为它们将自动默认为相同的值.因此,它假设如果您的代码注释了 DefaultValue
,那么 您的代码 将分配这些默认值.这可以在构造函数、字段初始化器或预反序列化回调中(支持所有 4 个标准回调点).
The DefaultValues
behavior (not just in protobuf-net - in System.ComponentModel
generally, for example PropertyGrid
, PropertyDescriptor
, etc) is that this is used to indicate things that don't need to be serialized, because they will be defaulted to that same value automatically. As such, it assumes that if your code annotates a DefaultValue
, then your code will be assigning those defaults. This could be in a constructor, field-intializer, or in a pre-deserialization callback (all 4 standard callback points are supported).
从技术上讲,库在反序列化之前显式分配这些值并非不可能 - 但简单地说:这不是当前编码的内容.
Technically, it would not be impossible for the library to explicitly assign these values prior to deserialization - but simply: that isn't what is coded currently.
这篇关于DefaultValue 属性反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!