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

Unable to enable non-public members in nested dictionary mapping. #256

Closed
miou-gh opened this issue Aug 5, 2020 · 2 comments
Closed

Unable to enable non-public members in nested dictionary mapping. #256

miou-gh opened this issue Aug 5, 2020 · 2 comments

Comments

@miou-gh
Copy link
Contributor

miou-gh commented Aug 5, 2020

    public class Pet
    {
        public string Name { get; set; }
        private string Color { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var config = new TypeAdapterConfig();
            config.ForType<IDictionary<string, object>, Pet>()
                .EnableNonPublicMembers(false);

            var pet = new Dictionary<string, object>() { { "Name", "Fluffy" }, { "Color", "White" } }.Adapt<Pet>(config);
        }
    }

If you change EnableNonPublicMembers to true it will make both members null. I believe the correct behavior would be to return an instance of Pet with both Name and Color initialized with their respective values.

@chaowlert
Copy link
Collaborator

Please annotate with [AdaptMember] for now. EnableNonPublicMembers will apply private lookup to both Pet and IDictionary. And this make Mapster lookup IDictionary private members rather than dictionary keys.

@chaowlert
Copy link
Collaborator

This is another way

 var config = new TypeAdapterConfig();
config.ForType<IDictionary<string, object>, Pet>()
                .IncludeMember((member, side) => side == MemberSide.Destination && 
                                                 member.Info is PropertyInfo &&
                                                 member.AccessModifier == AccessModifier.Private);

I will close this issue since there is workaround.

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