Skip to content

Dictionary projection by key not working #263

@YeskaNova

Description

@YeskaNova

Hi,
I need to select only some values to be copied from a dictionary but it doesn't work. You can reproduce it with this code:

Version: 6.1.1

Exception:
Mapster.CompileException
ArgumentException: 'Name' is not a member of type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]' (Parameter 'propertyOrFieldName')
System.Linq.Expressions.Expression.PropertyOrField(System.Linq.Expressions.Expression, string)
Mapster.Utils.ExpressionEx.PropertyOrField(System.Linq.Expressions.Expression, string)
System.Linq.Enumerable.Aggregate<TSource, TAccumulate>(System.Collections.Generic.IEnumerable, TAccumulate, System.Func<TAccumulate, TSource, TAccumulate>)
Mapster.Utils.ExpressionEx.PropertyOrFieldPath(System.Linq.Expressions.Expression, string)
Mapster.Models.InvokerModel.GetInvokingExpression(System.Linq.Expressions.Expression, Mapster.MapType)
Mapster.Models.InvokerModel.Next(System.Linq.Expressions.ParameterExpression, string)
Mapster.ReflectionUtils.Next.AnonymousMethod__1(Mapster.Models.InvokerModel)
System.Linq.Enumerable.WhereSelectListIterator<TSource, TResult>.MoveNext()
System.Linq.Enumerable.WhereEnumerableIterator.ToList()
System.Linq.Enumerable.ToList(System.Collections.Generic.IEnumerable)

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

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

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

            var p1 = new Post{ Dic = new Dictionary<string, object>() { { "Name", "test"}, {"Secret" , "password" }} };
            var p2 = new Post();
            mapper.From(p1).AdaptTo(p2);
            Debug.Assert(p1.Dic["Name"] == p2.Dic["Name"]);
            Debug.Assert(! p2.Dic.ContainsKey("Secret"));

        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions