You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi All,
Although the case is simple, I cannot get it to work.
I’m trying to serialize/deserialize (i.e. clone) an instance of a SubClass that derives a BaseClass. The baseclass has a private field which doesn’t seem to be (de-)serialized. I’m trying to avoid the annotation in the classes itself.
Bug description
Private members of base-class aren't (de-)serialized
Repro steps
public class BaseClass
{
private int _baseField;
public BaseClass() : this(-1)
{
}
public BaseClass(int value)
{
this.SetBaseField(value);
}
public int GetBaseField()
{
return _baseField;
}
public void SetBaseField(int value)
{
_baseField = value;
}
}
public class SubClass : BaseClass
{
private int _extField;
public SubClass():
this(-2, -2)
{
}
public SubClass(int baseField, int extField)
: base(baseField)
{
this.SetExtField(extField);
}
public int GetExtField()
{
return _extField;
}
public void SetExtField(int value)
{
_extField = value;
}
}
static void Main(string[] args)
{
var orgBase = new BaseClass(42);
var cloneBase = Clone(orgBase);
Console.WriteLine($"BaseClass org : [{orgBase.GetBaseField()}]");
Console.WriteLine($"BaseClass clone: [{cloneBase.GetBaseField()}]");
var orgSub = new SubClass(42, 73);
var cloneSub = Clone(orgSub);
Console.WriteLine($"SubClass org : [{orgSub.GetBaseField()}, {orgSub.GetExtField()}]");
Console.WriteLine($"SubClass clone : [{cloneSub.GetBaseField()}, {cloneSub.GetExtField()}]");
Console.ReadLine();
}
static T Clone<T>(T sut)
{
var options = TypelessContractlessStandardResolver.Options.WithResolver(
CompositeResolver.Create(
DynamicObjectResolverAllowPrivate.Instance,
ContractlessStandardResolverAllowPrivate.Instance,
TypelessContractlessStandardResolver.Instance));
byte[] seri = MessagePackSerializer.Serialize(sut, options);
var clone = MessagePackSerializer.Deserialize<T>(seri, options);
return (T)clone;
}
Expected behavior
The cloned object should have the value member int _baseField set to
BaseClass org : [42]
BaseClass clone: [42]
SubClass org : [42, 73]
SubClass clone : [42, 73]
Actual behavior
What happened instead of what you expected.
BaseClass org : [42]
BaseClass clone: [42]
SubClass org : [42, 73]
SubClass clone : [-2, 73]
Version used: 2.5.140
Runtime: .NET framework 4.8
Additional context
Is it me or a bug (or both).?
Thank you for your replies in advance,
The text was updated successfully, but these errors were encountered:
Hi All,
Although the case is simple, I cannot get it to work.
I’m trying to serialize/deserialize (i.e. clone) an instance of a SubClass that derives a BaseClass. The baseclass has a private field which doesn’t seem to be (de-)serialized. I’m trying to avoid the annotation in the classes itself.
Bug description
Private members of base-class aren't (de-)serialized
Repro steps
Expected behavior
The cloned object should have the value member int _baseField set to
BaseClass org : [42]
BaseClass clone: [42]
SubClass org : [42, 73]
SubClass clone : [42, 73]
Actual behavior
What happened instead of what you expected.
BaseClass org : [42]
BaseClass clone: [42]
SubClass org : [42, 73]
SubClass clone : [-2, 73]
Additional context
Is it me or a bug (or both).?
Thank you for your replies in advance,
The text was updated successfully, but these errors were encountered: