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

Transform the same instance #247

Closed
YeskaNova opened this issue Jun 6, 2020 · 0 comments
Closed

Transform the same instance #247

YeskaNova opened this issue Jun 6, 2020 · 0 comments

Comments

@YeskaNova
Copy link

I need to transform my entities before returning them to the caller. I need to replace sensitive fields/properties with Null and remove some keys from dictionaries. I have done a simple test and but I can't make it work, am I missing something? I want to replace the property Secret with null, and remove the Key "Secret" from the dictionary :

using Mapster;
using MapsterMapper;
using System.Collections.Generic;
using System.Diagnostics;

namespace ConsoleApp22
{
    public class Post
    {
        public IDictionary<string,string> Dic { get; set; }
        public string Secret { get; set; }
    }
    class Program
    {

        static void Main(string[] args)
        {
            var config = TypeAdapterConfig<Post, Post>
                .NewConfig()
                .Map(nameof(Post.Secret), x => default(string))
                .Map(nameof(Post.Dic) + ".Secret", x => default(string));
            config.Compile();
            var mapper = new Mapper(config.Config);

            var p1 = new Post() {Secret = "Test", Dic = new Dictionary<string, string>() { { "Secret", "test" }, {"B", "test2" } } };

            mapper.From(p1).AdaptTo(p1);
            Debug.Assert(p1.Dic["Secret"] == null);
            Debug.Assert(p1.Secret == null);

        }
    }
}

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

1 participant