Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Private fields of base-class doesn't get (de-)serialized. #1813

Closed
GunSharp opened this issue May 10, 2024 · 1 comment
Closed

Private fields of base-class doesn't get (de-)serialized. #1813

GunSharp opened this issue May 10, 2024 · 1 comment

Comments

@GunSharp
Copy link

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,

@AArnott
Copy link
Collaborator

AArnott commented May 10, 2024

Duplicate of #1756.

@AArnott AArnott closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants