.NET – A strange thing when using the inline initialization anti-sequence

When serializing a type with a boolean member initialized to true inline – I always get the value true when the object is deserialized (obviously, the problem is in the serialization The actual value of the previous member is false).
This situation will not happen in the opposite case (inline initialization is false, and the object value is true-I have also tried other operations and other data member types, and they all work. Normal work).

This is the type:

[ProtoContract]
public class SomeObject
{
public SomeObject() {}

[ProtoMember(1)]
private bool m_SomeMember = true;

public bool SomeMember
{
get {return m_SomeMember; }
set {m_SomeMember = value; }
}
}

This is the code:

var stream = new MemoryStream();
var data = new SomeObject() {SomeMember = false};
Serializer.SerializeWithLengthPrefix(stream, data, PrefixStyle.Base128);
stream.Position = 0;
var deserializedObject = Serializer.DeserializeWithLengthPrefix(stream, PrefixStyle.Base128);

When looking at the value of the deserializied object-SomeMember value is true.< br>This does not make much sense, because the inline member initialization code should be inserted into the default ctor by the compiler-as far as I know, protobuf should only be set after ctor is called Is it possible to set the value of the member (don’t think about it) So, help? (Slag?)

By default, protobuf-net uses an implicit zero default value (false is Treated as zero). You can tell it what you mean:

[ProtoMember(1), DefaultValue(true)]
private bool m_SomeMember = true ;

Or you can disable this behavior (via RuntimeTypeModel.UseImplicitZeroDefaults) so that only explicit default values ​​are processed.

When serializing a When connecting the type of a boolean member that is initialized to true-I always get the value true when the object is deserialized (obviously, the problem is that the actual value of the member is false before serialization).
This situation will not It happens in the opposite case (inline initialization is false, and the object value is true-I have also tried other operations and other data member types, and they all work fine).

This is the type :

[ProtoContract]
public class SomeObject
{
public SomeObject() {}

[ProtoMember (1)]
private bool m_SomeMember = true;

public bool SomeMember
{
get {return m_SomeMember; }
set {m_SomeMember = value; }
}
}

This is the code:

var stream = new MemoryStream();
var data = new SomeObject() {SomeMember = false};
Serializer.SerializeWithLengthPrefix(stream, data , PrefixStyle.Base128);
stream.Position = 0;
var deserializedObject = Serializer.DeserializeWithLengthPrefix(stream, PrefixStyle.Base128);

Viewing the value of the deserializied object When-SomeMember value is true.
This does not make much sense, because the inline member initialization code should be inserted into the default ctor by the compiler-as far as I know, protobuf should only set the value of the member after the ctor is called (don’t Think it) is it possible) so, help? (Slag?)

By default, protobuf-net uses an implicit zero default value (false is treated as zero). You can tell it what you mean :

[ProtoMember(1), DefaultValue(true)]
private bool m_SomeMember = true;

Or you can ( Disable this behavior via RuntimeTypeModel.UseImplicitZeroDefaults) so that only explicit default values ​​are processed.

Leave a Comment

Your email address will not be published.